Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

factory-girl

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

factory-girl - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

22

index.js

@@ -81,2 +81,24 @@ (function(factory) {

factory.sequence = function(fn) {
var result;
var sequenceNum = 1;
if (!fn || fn.length < 2) {
result = function() {
if (fn) {
return fn(sequenceNum++);
} else {
return sequenceNum++;
}
}
} else {
result = function(cb) {
return fn(sequenceNum++, cb);
}
}
return result;
};
factory.seq = factory.sequence;
factory.adapterFor = function(name) {

@@ -83,0 +105,0 @@ return adapters[name] || defaultAdapter;

2

package.json

@@ -6,3 +6,3 @@ {

"author": "Simon Wade",
"version": "2.0.0",
"version": "2.1.0",
"keywords": [

@@ -9,0 +9,0 @@ "factory",

@@ -7,4 +7,2 @@ # factory-girl

It started out as a fork of [factory-lady](https://github.com/petejkim/factory-lady), but the fork deviated quite a bit. This module uses an adapter to talk to your models so it can support different ORMs such as [Bookshelf](https://github.com/aexmachina/factory-girl-bookshelf), [Sequelize](https://github.com/aexmachina/factory-girl-sequelize), [JugglingDB](https://github.com/rehanift/factory-girl-jugglingdb), and [Mongoose](https://github.com/jesseclark/factory-girl-mongoose) (and doesn't use `throw` for errors that might occur during save).
## Installation

@@ -20,50 +18,76 @@

## Usage
```javascript
var factory = require('factory-girl');
var User = require('../models/user');
factory.define('user', User, {
username: 'Bob',
score: 50,
});
factory.build('user', function(err, user) {
console.log(user.attributes);
// => {username: 'Bob', score: 50}
});
```
## Defining Factories
```javascript
var factory = require('factory-girl'),
User = require('../../app/models/user'),
Post = require('../../app/models/post');
var factory = require('factory-girl');
var Post = require('../models/post');
var emailCounter = 1;
// define a factory using define()
factory.define('user', User, {
// define attributes using properties
state: 'active',
// ...or functions
email: function() {
return 'user' + emailCounter++ + '@demo.com';
email: factory.sequence(function(n) {
return 'user' + n + '@demo.com';
},
// provide async functions by accepting a callback
// async functions can be used by accepting a callback as an argument
async: function(callback) {
somethingAsync(callback);
},
// you can refer to other attributes using `this`
username: function() {
return this.email;
}
});
factory.build('user', function(err, user) {
console.log(user.attributes); // => {state: 'active', email: 'user1@demo.com', async: 'foo'}
console.log(user.attributes);
// => {state: 'active', email: 'user1@demo.com', async: 'foo', username: 'user1@demo.com'}
});
```
factory.define('comment', Comment, {
text: 'hello'
});
## Defining Associations
```javascript
factory.define('post', Post, {
// create associations using factory.assoc(model, key)
// or factory.assoc('user') to return the user object itself.
// create associations using factory.assoc(model, key) or factory.assoc('user') to return the user object itself.
user_id: factory.assoc('user', 'id'),
// create array of associations using factory.assocMany(model, key, num)
comments: factory.assocMany('comment', 'text', 2)
subject: 'Hello World',
// you can refer to other attributes using `this`
slug: function() {
return slugify(this.subject);
}
});
factory.build('post', function(err, post) {
console.log(post.attributes); // => {user_id: 1, comments: [{ text: 'hello' }, { text: 'hello' }],
// subject: 'Hello World', slug: 'hello-world'}
console.log(post.attributes);
// => { user_id: 1, comments: [{ text: 'hello' }, { text: 'hello' }] }
});
```
## Defining Sequences
```javascript
factory.define('post', Post, {
// Creates a new sequence that returns the next number in the sequence for
// each created instance, starting with 1.
num: factory.sequence(),
// factory.sequence can be abbreviated as factory.seq
email: factory.seq(function(n) {
return 'email' + n + '@test.com';
}),
// Can also be async
asyncProp: factory.seq(function(n, callback) {
somethingAsync(n, callback);
})
});
```
## Using Factories

@@ -75,7 +99,5 @@

});
factory.build('post', {title: 'Foo', content: 'Bar'}, function(err, post) {
// build a post and override title and content
});
factory.create('post', function(err, post) {

@@ -145,2 +167,15 @@ // post is a saved Post instance

## Like Promises?
Me too! Bluebird and q are both supported:
```javascript
var bluebird = require('bluebird');
var factory = require('factory-girl').promisify(bluebird);
```
## History
It started out as a fork of [factory-lady](https://github.com/petejkim/factory-lady), but the fork deviated quite a bit. This module uses an adapter to talk to your models so it can support different ORMs such as [Bookshelf](https://github.com/aexmachina/factory-girl-bookshelf), [Sequelize](https://github.com/aexmachina/factory-girl-sequelize), [JugglingDB](https://github.com/rehanift/factory-girl-jugglingdb), and [Mongoose](https://github.com/jesseclark/factory-girl-mongoose) (and doesn't use `throw` for errors that might occur during save).
## License

@@ -147,0 +182,0 @@

@@ -8,3 +8,3 @@ /* global describe, beforeEach, afterEach */

describe('factory', function() {
var Model, Person, Job;
var Model, Person, Job, Post;

@@ -31,2 +31,5 @@ before(function() {

Company.prototype = new Model();
Post = function() {};
Post.prototype = new Model();
});

@@ -65,2 +68,14 @@

});
factory.define('post', Post, {
num: factory.sequence(),
email: factory.sequence(function(n) {
return 'email' + n + '@test.com';
}),
name: factory.seq(function(n, cb) {
process.nextTick(function() {
cb(null, 'Post' + n);
});
})
});
});

@@ -135,2 +150,14 @@

});
context('factory containing a sequence', function() {
it('is able to handle that', function(done) {
factory.build('post', function(err, post) {
(post instanceof Post).should.be.true;
post.num.should.eql(1);
post.email.should.equal('email1@test.com');
post.name.should.equal('Post1');
done();
})
});
});
});

@@ -276,2 +303,9 @@ });

});
it('operates correctly with sequences', function(done) {
factory.buildMany('post', 3, function(err, posts) {
(posts[2].num - posts[1].num).should.eql(1);
(posts[1].num - posts[0].num).should.eql(1);
done();
});
});
});

@@ -278,0 +312,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc