Giter Site home page Giter Site logo

factory-lady's Introduction

build status

factory-lady.js

Factory-lady is a factory library for Node.js / JavaScript inspired by Factory_girl. It works asynchronously and supports lazy attributes as well as associations.

It works as long as new keyword is used on the model to instantiate new objects and save method is used to persist objects. For example, Mongoose models follow such convention.

Installation

Node.js:

npm install factory-lady

To use Factory-lady in the browser or other JavaScript environments, just copy and include factory-lady.js under lib directory.

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, {
  email    : function(cb) { cb('user' + emailCounter++ + '@example.com'); } // lazy attribute
, state    : 'activated'
, password : '123456'
});

Factory.define('post', Post, {
  user_id  : Factory.assoc('user', 'id') // simply Factory.assoc('user') for user object itself
, subject  : 'Hello World'
, content  : 'Lorem ipsum dolor sit amet...'
});

CoffeeScript:

Factory = require 'factory-lady'
User    = require '../../app/models/user'
Post    = require '../../app/models/post'

emailCounter = 1

Factory.define 'user', User,
  email    : (cb) -> cb("user#{emailCounter++}@example.com") # lazy attribute
  state    : 'activated'
  password : '123456'

Factory.define 'post', Post,
  user_id  : Factory.assoc 'user', 'id' # simply Factory.assoc 'user' for user object itself
  title    : 'Hello World'
  content  : 'Lorem ipsum dolor sit amet...'

Using Factories

JavaScript:

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

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

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

Factory('post', function(post) {
  // post is a saved Post instance
  // same as Factory.create
});

CoffeeScript:

Factory.build 'post', (post) ->
  # post is a Post instance that is not saved

Factory.build 'post', title: 'Foo', content: 'Bar', (post) ->
  # post is a Post instance that is not saved

Factory.create 'post', (post) ->
  # post is a saved Post instance

Factory 'post', (post) ->
  # post is a saved Post instance
  # same as Factory.create

License

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

factory-lady's People

Contributors

petejkim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

factory-lady's Issues

Config file?

Is there a way to create a single file or folder with the factories in them the way actory_girl allows?

Would be cool to be able to place a folder with multiple files or single file (maybe JSON format (factories.js?) with all the factories.

Something like:

test/
factories.js

or

test/
factories/
person.js
jobs.js
....

Thoughts?

Testing Default Values

I'm trying to test the default values on some Mongoose models. The test I'm using was basically making the object without the property in question and then testing if that value is added. However, this pattern does not seem to work with Factory Lady.

Is there a specific way to test defaults with Factory Lady? I already tried passing in { property_name: undefined } to Factory.build, to no avail.

Call stack exceeded for large data generation

I am using factory-girl together with Faker.js to generate sane large amounts of data, data is generated in an async manner, so i get the "call stack exceeded" message from factory-gril, the code bellow avoids that:

Avoid exceeding call stack:

    asyncForEach(hash.keys(attrs), function(key, cb) {
      var fn = attrs[key];
      if(typeof fn === 'function') {
        fn(function(value) {
          attrs[key] = value;
          setImmediate(function() { cb() })
        });
      } else {
        setImmediate(function() { cb() })
      }
    }, function() {
      var doc = new model();
      var key;
      for(key in attrs) {
        if(attrs.hasOwnProperty(key)) {
          doc[key] = attrs[key];
        }
      }
      setImmediate(function() { callback(doc) })
    });

You might improve it

Using with sequelize

Hi there,

I like girl grown up and became lady.

Is there any example of how you would write fixtures with Sequelize?

Thanks,

Sanjay

How do you handle references to other documents/tables?

I've got a One-to-One relationship between a User model and a few role models such as Admin, Manager, and Customer. How might you handle a scenario like this? I'm using Mongoose, but I believe the same principles apply for any One-to-One relationship.

User Schema

var UserSchema = new Schema({
  email: {
    // ...
  },
  password: {
    // ...
  },
  roles: {
    admin: { type: mongoose.Schema.Types.ObjectId, ref: 'Admin' },
    manager: { type: mongoose.Schema.Types.ObjectId, ref: 'Manager' },
    customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer' }
  },
  salt: {
    // ...
  },
  updated: {
    // ...
  },
  created: {
    // ...
  }
});

Admin Schema

var AdminSchema = new Schema({
  user: {
    id: { type: Schema.Types.ObjectId, ref: 'User' },
    name: { type: String, default: '' }
  },
  created: {
    // ...
  }
});

subdocuments

Is there a way to create subdocuments with factoryLady. For some reason I keep getting a null return.

faker = require "Faker"

Upload = app.models.broadcasts.upload

factoryLady.define "upload", Upload,

bucket: "jumper-ios-test"
region: "us-west-1"
bucketKey: "2DE45F47-6782-4532-8C4C-2B4285BAC84E"
meta:

    test: faker.Lorem.words(3).join ""

This will have meta = null in my returned object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.