js-factories

Object factory to create dynamic test fixtures


Keywords
test, fixtures, factory, testing, traits, sequences
License
MIT
Install
npm install js-factories@1.1.1

Documentation

js-factories

Build Status NPM Version

js-factories is a library to use dynamic fixtures using Factories in javascript/coffeescript. Ideal to combine using Mocha/chai frameworks and test rich classes for MV** Frameworks like Backbone.js

Usage

Include js-factories.js in your test suite.

Factory support is added to quickly be able to build models or other objects as you see fit:

  Factory.define('user', function(attributes) {
    if (attributes == null) {
      attributes = {};
    }
    return new User(attributes);
  });

  Factory.create('user', { name: 'Matthijs' })
  Factory.createList(10, 'user', { name: 'Matthijs' })

Traits

you can also use 'traits'. Traits are flags that are set when the user calls create with the factory name prefixed with terms separated by dashes.

Like: 'female-admin-user'

This will call the 'user' factory, and provide the terms 'female' and 'admin' as traits for this user

this list is accessible in the factory callback using this.traits

There are 2 helper methods to help check if traits are set:

this.trait('returns', 'one', 'of', 'these', 'values')

and

this.is('admin') // returns a boolean value

Extended example:

  Factory.define('user', function(attributes) {
    var returningClass;
    if (attributes == null) {
      attributes = {};
    }
    attributes.gender = this.trait('male', 'female') || 'male';
    returningClass = User;
    if (this.is('admin')) {
      returningClass = AdminUser;
    }
    return new returningClass(attributes);
  });

  Factory.create('user', { name: 'Matthijs' }) // => new User name: 'Matthijs'
  Factory.create('male-user', { name: 'Matthijs' }) // => new User name: 'Matthijs', gender: 'male'
  Factory.create('male-admin-user', { name: 'Matthijs' }) // => new AdminUser name: 'Matthijs', gender: 'male'
  Factory.create('female-user', { name: 'Beppie' }) // => new User name: 'Beppie', gender: 'female'

Sequences

Sequences are also supported:

  Factory.define('counter', function() {
    return {
      amount: this.sequence('amount'),
      other: this.sequence('other')
    };
  });

This does not conflict with similar names in other factory definitions.

You can also yield results:

  Factory.define('abc', function() {
    return this.sequence(function(i) {
      return ['a', 'b', 'c'][i];
    });
  });

  // results in:
  Factory.create('abc') // => 'a'
  Factory.create('abc') // => 'b'

Sampling

You can sample a value from a list

  Factory.define('sampler', function() {
    return this.sample('a', 'b', 'c');
  });

Will randomly return a, b or c every time

License

Copyright (c) 2012-2014 Matthijs Groen

MIT License (see the LICENSE file)