A library for create test-data factories
You can install the Factory.js using npm the following way:
npm install factory.js
You can require the Factory.js using require.js the following way:
define('path/to/factory', function (factory) {
// define you're module that uses the factory
});
If you choose to use the library directly in the browser the factory will be installed in the global namespace under the name:
weknowhow.factory
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' }
]
You create a new factory the following way:
var personFactory = factory(function (name) {
return new Person({ name: (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.
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.
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.
All additional arguments to create will be forwarded to the factory method:
var persons = personFactory.create(2, 'Human');
This will create a persons array containing two persons.
person[0]
will be named Human0 and person[1]
will be named Human1.
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.
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');
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.
Gert Sønderby (@gertsonderby)
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.