Skip to content
Node.js library for creating objects from factories, inspired by factory_girl.
Find file

Factory Boy

Build Status Coverage Status NPM version

Factory Boy is an library for Node.js which provides factories for objects creation. It's highly inspired by the fabulous factory_girl library for Ruby on Rails.

It comes with support for :

  • associations
  • lazy attributes
  • defining factory initialization and creation methods


npm install factory-boy

Defining factories

Factory = require('factory-boy')

Factory.define 'user', class: User, ->
  @first_name = 'John'
  @last_name = 'Smith'
  @pin = (callback) -> callback(null, Math.floor(Math.random()*10000)) 'user', (err, user) ->
  console.log user

Factory.create 'user', (err, user) ->
  console.log user

Custom intialization and creation methods

Factory Boy use initializeWith and createWith methods for building and creating factory objects :

initializeWith: (klass, attributes, callback) ->
  callback(null, new klass(attributes))

createWith: (klass, attributes, callback) ->
  klass.create(attributes, callback)

You can overwrite this methods on global level or per each factory :

# overwriting globally
Factory = require('factory-boy')

Factory.initializeWith = (klass, attributes, callback) ->
  callback(null, new

Factory.createWith = (klass, attributes, callback) ->

# overwriting per factory
Factory.define 'user', class: User, ->
  @initializeWith = (klass, attributes, callback) ->

  @createWith = (klass, attributes, callback) ->

Lazy attributes

Attributes defined by functions are evaluated upon object intialization/creation. Lazy functions context are set to factory instance so it's possible to use already defined attributes.

Factory.define 'user', class: User, ->
  @first_name = 'John'
  @salt = (callback) ->
    time = new Date()
    callback(null, "#{first_name}#{time}")

Additionally lazy functions are evaluated in the defined order.

Factory.define 'user', class: User, ->
  @number1 = (callback) -> callback(null, 10)
  @number2 = (callback) -> callback(null, @number1 + 10)


Sequences can be used for creating record with unique attributes i.e. emails. They are creating lazy attribute for given field with iterator passed as first argument.

Factory.define 'user', class: User, ->
  @sequence 'email', (n, callback) ->
    callback(null, "test#{n}")

First variable in callback will be increment for each records, starting from value 1. Therefore creating user factories will return records with unique emails.


Factory.define 'user', class: User, ->
  @first_name = 'John'

Factory.define 'profile', class: Profile, ->
  @avatar_url = ''

Factory.create 'user', (err, user) ->
  console.log user.profile_id

When using associations you can pass field name as first parameter.

Factory.define 'user', class: User, ->
  @first_name = 'John'
  @association('user_profile_id', 'profile')

Also you can set values to associated factory.

Factory.define 'user', class: User, ->
  @first_name = 'John'
  @association('profile', avatar_url: '')

By default Factory Boy will use id field from associated factory. This can be changed by passing factory options.

Factory.define 'user', class: User, ->
  @first_name = 'John'
  @association('profile', factory: {field: 'external_id'})


  1. Fork it
  2. Install dependencies

    npm install
  3. Make your changes on branch and make sure all tests pass

    npm test
  4. Submit your pull request !

Something went wrong with that request. Please try again.