Giter Club home page Giter Club logo

factory-girl's Introduction

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, Sequelize, JugglingDB, and Mongoose (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-girl'),
    User    = require('../../app/models/user'),
    Post    = require('../../app/models/post');

var emailCounter = 1;

// define a factory using define()
factory.define('user', User, {
  // define attributes using properties
  state: 'active',
  // ...or functions
  email: function() {
    return 'user' + emailCounter++ + '@demo.com';
  },
  // provide async functions by accepting a callback
  async: function(callback) {
    somethingAsync(callback);
  }
});
console.log(factory.build('user')); // => {state: 'active', email: '[email protected]', async: 'foo'}

factory.define('post', Post, {
  // create associations using factory.assoc(model, key)
  // or factory.assoc('user') to return the 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: 1, subject: 'Hello World', slug: 'hello-world'}

Factory#assoc

You can optionally provide attributes to the associated factory by passing an object as third argument.

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
});

Factory#buildMany

Allow you to create a number of models at once.

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
});
factory.buildMany('post', {title: 'Foo'}, 10, function(err, posts) {
  // build 10 posts using the specified attributes for all of them
});

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'});

Creating new Factories and Adapters

Adapters provide support for different databases and ORMs.

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

Or use the ObjectAdapter that simply returns raw objects.

var ObjectAdapter = factory.ObjectAdapter;
anotherFactory.setAdapter(ObjectAdapter, 'post'); // use the ObjectAdapter for posts

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.