test-factory

0.0.1 • Public • Published

Factory.js

A library for create test-data factories

Build Status

Usage

function Game(data) {
    this.id = data.id;
    this.isOver = data.isOver;
    this.createAt = data.createAt;
    this.randomSeed = data.randomSeed;
    this.players = data.players;
}

function Player(data) {
    this.id = data.id;
    this.name = data.name;
}

var playerFactory = factory(function (name) {
    var id = this.sequence();
    name = name || 'Player ' + id;
    return new Player({
        id: id,
        name: name
    });
});

var gameFactory = factory(function () {
    var players = playerFactory.create(2);
    players.push(playerFactory('Awesome player'));
    return new Game({
        id: this.sequence(),
        isOver: false,
        createAt: new Date(),
        randomSeed: Math.random(),
        players: players
    });
});

You can now build a new game the following way:

var game = gameFactory();

which returns a Game instance with the following data:

id: 0,
isOver: true,
createdAt: Wed Apr 03 2013 21:56:16 GMT+0200 (CEST),
randomSeed: 0.672447538934648,
players: [
    { id: 0, name: 'Player 0' },
    { id: 1, name: 'Player 1' },
    { id: 2, name: 'Awesome player' }
]

Create a new factory

You create a new factory the following way:

var personFactory = factory(function () {
    return new Person({ name: 'Person' + this.sequence() });
});

This effectively binds the given function to a factory instance that provides support for sequencing. Furthermore the given function is decorated with methods for creating multiple instances and reset the sequence of the factory.

Creating an instance using the factory

You create a new instance by calling the factory:

var person0 = personFactory();
var person1 = personFactory();

person0 will be named Person0 and person1 will be named Person1.

Creating multiple instances using the factory

You can create multiple instances using the create method on the factory:

var persons = personFactory.create(2);

This will create a persons array containing two persons. person[0] will be named Person0 and person[1] will be named Person1.

Parameterized factories

You can parameterize you factory the following way:

var personFactory = factory(function (name) {
    name = name || 'Person' + this.sequence();
    return new Person({ name: name });
});

When you call the personFactory with a name it will return a person with that name; otherwise it will default to the sequenced name.

var person0 = personFactory();
var person1 = personFactory('foo');

person0 will be named Person0 and person1 will be named Foo.

If you use the create method to create multiple instances it call the factory method with no parameters.

Combining factory methods

You are free to combine factory methods as you are pleased. But be aware not to create cyclic recursion.

var dogFactory = factory(function () {
    return new Dog({ name: 'Dog' + this.sequence() });
});

var personFactory = factory(function () {
    return new Person({ 
        name: 'Person' + this.sequence(), 
        dog: dogFactory();
    });
});

Every person you create with the personFactory will have a dog instance associated.

var person0 = personFactory();
var person1 = personFactory();

assert(person0.name === 'Person0');
assert(person0.dog.name === 'Dog0');

assert(person1.name === 'Person1');
assert(person1.dog.name === 'Dog1');

Resetting the sequence of a factory

You can reset the sequencing of a factory the following way:

var personFactory = factory(function () {
    return new Person({ name: 'Person' + this.sequence() });
});

var person0 = personFactory();
personFactory.reset();
var person1 = personFactory();

person0 will be named Person0 and person1 will be named Person0.

Contributors

Gert Sønderby (@gertsonderby)

License

Copyright 2013 Sune Simonsen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i test-factory

    Weekly Downloads

    2

    Version

    0.0.1

    License

    none

    Last publish

    Collaborators

    • sunesimonsen