factory-girl
factory-girl
is a factory library for Node.js / JavaScript inspired by Factory_girl. It works asynchronously and supports lazy attributes as well as associations.
It is based on factory-lady, but uses an adapter to talk to your models (and doesn't use throw
for errors that might occur during save).
Installation
Node.js:
npm install factory-girl
To use factory-girl
in the browser or other JavaScript environments, just include index.js
.
Defining Factories
JavaScript:
var factory = require('factory-lady'),
User = require('../../app/models/user'),
Post = require('../../app/models/post');
var emailCounter = 1;
factory.define('user', User, {
email : function() { return 'user' + emailCounter++ + '@example.com'; },
async : function(cb) { somethingAsync(cb); },
state : 'activated',
password : '123456'
});
factory.define('post', Post, {
user_id : factory.assoc('user', 'id')
subject : 'Hello World',
content : 'Lorem ipsum dolor sit amet...'
});
Using Factories
JavaScript:
factory.build('post', function(err, post) {
});
factory.build('post', {title: 'Foo', content: 'Bar'}, function(err, post) {
});
factory.create('post', function(err, post) {
});
factory('post', function(err, post) {
});
License
Copyright (c) 2011 Peter Jihoon Kim. This software is licensed under the MIT License.
Copyright (c) 2014 Simon Wade. This software is licensed under the MIT License.