Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic properties #26

Open
dkniffin opened this issue Nov 4, 2017 · 1 comment
Open

Allow dynamic properties #26

dkniffin opened this issue Nov 4, 2017 · 1 comment

Comments

@dkniffin
Copy link

dkniffin commented Nov 4, 2017

The biggest reason to use factories instead of fixtures is that the properties are not static, so your tests aren't depending on static "magic" values. This article explains pretty well why thoughtbot's factory_girl gem is better than the built-in rails fixtures.

For this reason, I think this library should support generating dynamic properties.

For example:

chai.factory('person', { 
  age: () => { Math.floor(Math.random() * (100 - 5)) + 5) } // Pick a random age between 5 and 100.
});

var user1 = chai.create('person');
var user2 = chai.create('person');
console.log(user1.age); // Logs 31
console.log(user2.age); // Logs 57
@dwoowb
Copy link

dwoowb commented Jul 3, 2018

Ran into this as well, this is the way I got your desired effect (I use FakerJS in my example):

const factoryData = () => ({
  id: faker.random.uuid()
});

chai.factory('myModel', {});

const myModelFactory = (opts) => (
  chai.create(
    'myModel',
    {
      ...factoryData(),
      ...opts
    }
  )
);

module.exports = myModelFactory;

Also, this way I keep my factory modularized for import wherever it's needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants