Giter Site home page Giter Site logo

factory-boy's Introduction

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

Installation

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

Factory.build '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 klass.build(attributes))

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

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

  @createWith = (klass, attributes, callback) ->
    new klass.build(attributes).create(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

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}@example.com")

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

Associations

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

Factory.define 'profile', class: Profile, ->
  @avatar_url = 'http://example.com/img.png'

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: 'http://example.com/img2.png')

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

Contributing

  1. Fork it
  2. Install dependencies
npm install
  1. Make your changes on branch and make sure all tests pass
npm test
  1. Submit your pull request !

factory-boy's People

Contributors

kbackowski avatar

Watchers

Jack Danger avatar James Cloos avatar  avatar

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.