Socket
Socket
Sign inDemoInstall

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

A factory library for javascript / node.js inspired by factory_girl


Version published
Weekly downloads
43K
decreased by-2.64%
Maintainers
1
Weekly downloads
 
Created
Source

factory-girl

factory-girl is a factory library for Node.js and the browser that is inspired by Factory_girl. It works asynchronously and supports associations and the use of functions for generating attributes.

It started out as a fork of 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 and Sequelize (and doesn't use throw for errors that might occur during save).

Installation

Node.js:

npm install factory-girl

To use factory-girl in the browser or other JavaScript environments, just include index.js and access window.Factory.

Defining Factories

JavaScript:

var factory = require('factory-lady'),
    User    = require('../../app/models/user'),
    Post    = require('../../app/models/post');

var emailCounter = 1;

factory.define('user', User, {
  state: 'activated',
  // define attributes using functions
  email: function() {
    return 'user' + emailCounter++ + '@example.com';
  },
  // or using async functions by accepting a callback
  async: function(callback) {
    somethingAsync(callback);
  },
  password: '123456'
});

factory.define('post', Post, {
  // create associations using factory.assoc(model, attr)
  // or factory.assoc('user') for user object itself
  user_id: factory.assoc('user', 'id'),
  subject: 'Hello World',
  content: 'Lorem ipsum dolor sit amet...',
  // you can refer to other attributes using `this`
  slug: function() {
    return slugify(this.subject);
  }
});

Using Factories

JavaScript:

factory.build('post', function(err, post) {
  // post is a Post instance that is not saved
});

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

factory.create('post', function(err, post) {
  // post is a saved Post instance
});

factory('post', function(err, post) {
  // same as factory.create
});

License

Copyright (c) 2011 Peter Jihoon Kim. This software is licensed under the MIT License. Copyright (c) 2014 Simon Wade. This software is licensed under the MIT License.

Keywords

FAQs

Package last updated on 22 May 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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