factory-girl
factory-girl
is a factory library for Node.js and the browser that is inspired by Factory_girl. It works asynchronously and supports associations and the use of functions for generating attributes.
Installation
Node.js:
npm install factory-girl
To use factory-girl
in the browser or other JavaScript environments, just include index.js
and access window.Factory
.
Usage
var factory = require('factory-girl');
var User = require('../models/user');
factory.define('user', User, {
username: 'Bob',
score: 50,
});
factory.build('user', function(err, user) {
console.log(user.attributes);
});
Defining Factories
var factory = require('factory-girl');
var User = require('../models/user');
factory.define('user', User, {
email: factory.sequence(function(n) {
return 'user' + n + '@demo.com';
}),
async: function(callback) {
somethingAsync(callback);
},
username: function() {
return this.email;
}
});
factory.build('user', function(err, user) {
console.log(user.attributes);
});
Options
Options can be provided when you define a model:
factory.define('user', User, { foo: 'bar' }, options);
Alternatively you can create a new factory that specifies options for all of its models:
var builder = factory.withOptions(options);
Currently the supported options are:
afterBuild: function(instance, options, callback)
Provides a function that is called after the model is built.
afterCreate: function(instance, options, callback)
Provides a function that is called after a new model instance is saved.
Defining Associations
factory.define('post', Post, {
user_id: factory.assoc('user', 'id'),
comments: factory.assocMany('comment', 'text', 2)
});
factory.build('post', function(err, post) {
console.log(post.attributes);
});
Defining Sequences
factory.define('post', Post, {
num: factory.sequence(),
email: factory.seq(function(n) {
return 'email' + n + '@test.com';
}),
asyncProp: factory.seq(function(n, callback) {
somethingAsync(n, callback);
})
});
Using Factories
Factory#build
Creates a new (unsaved) instance.
factory.build('post', function(err, post) {
});
factory.build('post', {title: 'Foo', content: 'Bar'}, function(err, post) {
});
Factory#create
Builds and saves a new instance.
factory.create('post', function(err, post) {
// post is a saved Post instance
});
Factory#assoc
You can optionally provide attributes to the associated factory by passing an object as third argument.
Factory#buildMany
Allow you to create a number of models at once.
factory.buildMany('post', 10, function(err, posts) {
});
factory.buildMany('post', [{title: 'Foo'}, {title: 'Bar'}], function(err, posts) {
});
factory.buildMany('post', [{title: 'Foo'}, {title: 'Bar'}], 10, function(err, posts) {
});
factory.buildMany('post', {title: 'Foo'}, 10, function(err, posts) {
});
Factory#createMany
factory.createMany
takes the same arguments as buildMany
, but returns saved models.
Factory#buildSync
When you have factories that don't use async property functions, you can use buildSync()
.
Be aware that assoc()
is an async function, so it can't be used with buildSync()
.
var doc = factory.buildSync('post', {title: 'Foo'});
Factory#cleanup
Destroys all of the created models. This is done using the adapter's destroy
method.
Adapters
Adapters provide support for different databases and ORMs.
Adapters can be registered for specific models, or as the 'default adapter', which is used for any models for which an adapter has not been specified.
See the adapter docs for usage, but typical usage is:
require('factory-girl-bookshelf')();
factory.setAdapter(new factory.ObjectAdapter(), 'post');
Creating new Factories
You can create multiple factories which have different settings:
var anotherFactory = new factory.Factory();
var BookshelfAdapter = require('factory-girl-bookshelf').BookshelfAdapter;
anotherFactory.setAdapter(new BookshelfAdapter());
Like Promises?
Me too! Bluebird and q are both supported:
var bluebird = require('bluebird');
var factory = require('factory-girl').promisify(bluebird);
History
It started out as a fork of factory-lady, but the fork deviated quite a bit. This module uses an adapter to talk to your models so it can support different ORMs such as Bookshelf, Sequelize, JugglingDB, and Mongoose (and doesn't use throw
for errors that might occur during save).
License
Copyright (c) 2014 Simon Wade. This software is licensed under the MIT License.
Copyright (c) 2011 Peter Jihoon Kim. This software is licensed under the MIT License.