Open dkniffin opened 6 years ago
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.
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: