Factory Girl
port features from factory_girl in rails to nodejs
Installation
Node.js:
npm install 'factory_girl'
Usage
Define factory
var FactoryGirl = ; // for nodejs
FactoryGirl
Using factories
user = FactoryGirl; // create instance useruserattributes; // => {id: 1, title: 'That\'s awesome day', emotion: 'happy'}
It's possible to override the defined attributes by passing a json:
user = FactoryGirl;userattributes; // => {id: 2, ....}FactoryGirl // => {id: 1, ...}FactoryGirl // => true
Loading factories
When using node.js you can specify the path(s) to your factories, so FactoryGirl will autoload all factories in that paths
FactoryGirldefinitionFilePaths = __dirname + '/factories'; FactoryGirl;
* The path to the factories must be an Array
and also must be relative to the current file
Aliases
FactoryGirl
FactoryGirl.create('user')
is equal to FactoryGirl.create('doctor')
another thing is you can pass to array to alias {alias: ['doctor', 'patient']
Inheritance
FactoryGirlvar doctor = Factory;doctorattributes // => {id: 2, title: 'That\'s awesome day', emotion: 'happy', label: 'Dr'}
Association
FactoryGirlFactoryGirlFactoryGirl
name
: Factory's property name
factoryName
: Definition name of the related factory
num
: Number of related factories to be created
ref
: Foreign key attribute name
Examples
FactoryGirl FactoryGirl FactoryGirl var profile = FactoryGirl;profileattributes // => {label: 'Dr'};profile // => {id: 2, label: 'Dr', user_id: 1, user: {id: 1, title: 'That\'s awesome day', emotion: 'happy'}} var place = FactoryGirl;profile // => {id: 2, label: 'New York', user: {id: 1, place_id: 2, ...}}
Create Multi Object
FactoryGirl id: 1 ... id: 23 ...
Sequences
FactoryGirl;FactoryGirlvar user = FactoryGirl;user // => {name: 'name 1'}var user2 = FactoryGirl;user2 // => {name: 'name 2'}