rehanift / factory-girl

A factory library for node.js and the browser inspired by factory_girl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

factory-girl

Build Status

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.

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 and Sequelize (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 and access window.Factory.

Defining Factories

var factory = require('factory-lady'),
    User    = require('../../app/models/user'),
    Post    = require('../../app/models/post');

var emailCounter = 1;

factory.define('user', User, {
  state: 'active',
  // define attributes using functions
  email: function() {
    return 'user' + emailCounter++ + '@example.com';
  },
  // or using async functions by accepting a callback
  async: function(callback) {
    somethingAsync(callback);
  },
  password: '123456'
});
console.log(factory.build('user')); => {state: 'active', email: 'user1@example.com', async: 'foo', password: '123456'}

factory.define('post', Post, {
  // create associations using factory.assoc(model, attr)
  // or factory.assoc('user') for user object itself
  user_id: factory.assoc('user', 'id'),
  subject: 'Hello World',
  // you can refer to other attributes using `this`
  slug: function() {
    return slugify(this.subject);
  }
});
console.log(factory.build('post')); => {user_id: 123, subject: 'Hello World', slug: 'hello-world'}

Using Factories

factory.build('post', function(err, post) {
  // post is a Post instance that is not saved
});

factory.build('post', {title: 'Foo', content: 'Bar'}, function(err, post) {
  // build a post and override title and content
});

factory.create('post', function(err, post) {
  // post is a saved Post instance
});

buildMany and createMany

factory.buildMany('post', 10, function(err, posts) {
  // build 10 posts
});
factory.buildMany('post', [{title: 'Foo'}, {title: 'Bar'}], function(err, posts) {
  // build 2 posts using the specified attributes
});
factory.buildMany('post', [{title: 'Foo'}, {title: 'Bar'}], 10, function(err, posts) {
  // build 10 posts using the specified attributes for the first and second
});

Creating new Factories and Adapters

var anotherFactory = new factory.Factory();
var BookshelfAdapter = require('factory-girl-bookshelf').BookshelfAdapter;
anotherFactory.setAdapter(BookshelfAdapter); // use the Bookshelf adapter

// the ObjectAdapter simply returns raw objects
var ObjectAdapter = require('factory-girl/lib/object-adapter');
anotherFactory.setAdapter(ObjectAdapter, 'post'); // use the ObjectAdapter for posts

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.

About

A factory library for node.js and the browser inspired by factory_girl

License:MIT License