node-factories
v0.0.1
Published
A library for creating test objects in node.js
Downloads
512
Readme
Node-factories is used to create test data in a simple way. It is inspired by factory_girl which is used in the ruby-world.
Similar to factory_girl, it supports multiple build strategies (build(), attributes() and create()), sequences and traits to cover the typical requirements for generating test data.
Installation
Node.js:
npm install node-factories
Simple factories
Simple definition of a user:
var factories = require('factories');
factories.define('user', {
firstName: 'John',
lastName: 'Doe',
isAdmin: false
});
// build a user:
factories.user.build();
// build an array of 10 users
factories.user.build(10);
Building strategies
There are different strategies to create a new object: 'attributes()' creates an Object with the attributes defined in the factory definition; 'build()' creates an Object with the prototype of the factory definition and 'create()' calls the 'create()' method on each object.
factories.define('user', {
//...
});
typeof factories.user.attributes();
// => 'object'
typeof factories.user.build();
// => 'object'
var User = function() {};
factories.define('userWithPrototype', User, {
//...
});
typeof factories.user.attributes();
// => 'object'
typeof factories.user.build();
// => 'function'
Lazy attributes
Instead of assigning static values to the attributes, lazy attributes can be defined. In this case, the attribute value in the definition of the object is a function, which is evaluated whenever an object is created.
factories.define('user', {
// ...
createdAt: function() { return new Date(); }
});
Sequences
Sequences are used to generate unique values (as opposed to the users above, which all have the same name). Basically, node-factories maintains a counter for each sequence which is increased anytime an object is created. This counter is passed to a function, which determines the actual attribute value.
var factories = require('factories');
factories.define('userWithMail', {
firstName: 'John',
lastName: 'Doe'
})
.sequence('email', function(i) {return 'person_' + i + '@example.com'});
// sequence starts with i=0:
factories.userWithMail.build().email
// => [email protected]
factories.userWithMail.build().email
// => [email protected]
Sequences can also be defined globally, i.e. they can be used in different factories.
factories.sequence('email', function(i) {return 'person_' + i + '@example.com'});
factories.define(userWithMail, {
// ...
})
.sequence('email');
factories.define(otherObject, {
// ...
})
.sequence('email');
// the sequence is now increased globally:
factories.userWithMail.build().email
// => [email protected]
factories.otherObject.build().email
// => [email protected]
Traits
You can use traits for advanced configuration:
var factories = require('factories');
factories.define('user', {
firstName: 'John',
lastName: 'Doe',
isAdmin: false
}).trait({
'admin',
{ isAdmin: true }
});
// build a normal user:
factories.user.build();
// and an admin:
factories.user.trait('admin').build();