Giter Site home page Giter Site logo

Comments (4)

chetanism avatar chetanism commented on June 23, 2024

Hi @Kadrian, the library relies on DB to persist models, which usually is asynchronous. You may instead write multiple before blocks. e.g.

before((done) => {
  factory.create('user', () => { done(); }
});
before((done) => {
  factory.create('client', () => { done(); }
});
...

Otherwise, you may promisify the library either using bluebird or es6-promisify, and then do:

before((done) => {
  const userP = factory.create('user');
  const clientP = factory.create('client');
  const booksP = factory.createMany('book');
  Promise.all([userP, clientP, booksP]).then(() => done());
}

If you can use es7 async/await via babel, you may then also do (after promisifying):

before(async (done) => {
  const user = await factory.create('user');
  const client = await factory.create('client');
  const books = await factory.createMany('book');
  done();
}

from factory-girl.

Kadrian avatar Kadrian commented on June 23, 2024

Aha, thanks @chetanism, that's what I was looking for! I think this totally belongs into the README! I didn't know that I HAD to "promisify" the library to really leverage its power. What's the rationale for not making this concise code the default / suggested way of using factory-girl? If it's because there are multiple implementations for promises and you would like people to be able to choose, I understand. But now I think people are forced to choose before they're able to actually use all the power of this library.
In any case, I wish this was being made a little clearer for beginners like me 😁 Thanks again!

from factory-girl.

chetanism avatar chetanism commented on June 23, 2024

This library was originally forked from factory-lady, and continues to keep its API interface. However, we have been working on rewriting the library, keeping promises by default as you suggest!
You can take a sneak-peek at https://github.com/chetanism/factory-girl/tree/rewrite. The work is almost complete, but we still need to update the documentation. It would be great if you can provide some feedback or try it out in a dummy project (since some APIs are not compatible with current version) and report any bugs that you may find.
Once we feel the new library is ready for a release (in probably 2-4 weeks), we'll roll out a brand new v4!

from factory-girl.

simonexmachina avatar simonexmachina commented on June 23, 2024

factory-girl 4.0.0-beta.1 is now available. With the new version you can:

before(() => 
  factory.create('user')
    .then(() => factory.create('client'))
);

from factory-girl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.