/Backbone-Factory

A factory for creating backbone objects for testing

Primary LanguageJavaScript

Backbone Factory

Introduction

Backbone Factory is a small javascript library for creating Backbone.js objects for testing your code. We have extended this library to provide support from nested models/collections. This is a strict superset of the original authors functionality. It has no external dependency.

The API is heavily inspired by the awesome Factory Girl.

Installation

To use it, just download the file and include it in your testing setup. Usage

Let's say you have two Backbone models - Post and User

var User = Backbone.Model.extend({

  defaults: {
    name: 'Raghu',
    email: 'raghu@example.com'
  }

});

var Post = Backbone.Model.extend({

  defaults: {
    title: 'Default Title'
  }

});

Defining Factories

To define factories for them

BackboneFactory.define('post', Post);
BackboneFactory.define('user', User);

Using Factories

To use these factories,

var postObject = BackboneFactory.create('post');
var userObject = BackboneFactory.create('user');

This will create objects using the defaults you have in your class definitions. It is easy to override the defaults when creating an object

var userObject = BackboneFactory.create('user', function(){
    return {
        name: "Custom Name",
        email: "custom@email.com"
    };
});

Defining and using Collections

To define factories for collections

var Posts = Backbone.Collection.extend({
      model: Post
});
var Users = Backbone.Collection.extend({
      model: User
});

BackboneFactory.define_collection('posts', Posts, 3); //Third arugement is the default size of the collection
BackboneFactory.define_collection('users', Users, 4);

var postsCollection = BackboneFactory.create_collection('posts'); //Creates 3 posts
var usersCollection = BackboneFactory.create_collection('users',2) //Creates only 2 users overriding the default of 4

Schema Support

If your backbone models define a schema, it is used to set default values. Nested models are also supported.

var PostWithSchema = Backbone.Model.extend({

  schema: {
    title: {
      type: 'string',
      default: 'Default value from schema'
    },
    body: {
      type: 'string',
      default: 'Default body'
    },
    author: {
      type: 'related',
      _constructor: User
    }
  }

});


BackboneFactory.define('post_with_schema', PostWithSchema);
var post = BackboneFactory.create('post_with_schema');

//Post title will be picked up from schema
console.log(post.get('title'); //Default value from schema

//Author was created automatically using the schema
var author = post.get('author');
console.log(author.get('name'); //Raghu

Cool! isn't it? If defaults and schema are both present, default values from the backbone model take precedence over the schema defaults. Now, lets complicate this a bit further.

var Comment = Backbone.Model.extend({

  schema: {
    msg: {
      type: 'string',
      default: 'Default comment msg'
    }
  }

});

var Comments = Backbone.Collection.extend({
  model: Comment
});

var PostWithSchema = Backbone.Model.extend({

  schema: {
    title: {
      type: 'string',
      default: 'Default value from schema'
    },
    body: {
      type: 'string',
      default: 'Default body'
    },
    author: {
      type: 'related',
      _constructor: User
    }
    comments: {
      type: 'related',
      _constructor: Comments
    }
  }
});


BackboneFactory.define('post_with_schema', PostWithSchema);
BackboneFactory.define('comments', Comments, 2);

var post = BackboneFactory.create('post_with_schema');
var comments = post.get('comments');
console.log(comments.length) //gives a value of 2

Its very easy to change the size of the collection of comments

var post = BackboneFactory.create('post_with_schema', function(){
    return { comments: BackboneFactory.create_collection('comments', 4) };
});
var comments = post.get('comments');
console.log(comments.length) //gives a value of 4

If a comments BackboneFactory is not defined, an empty collection of comments is created when a post is created.

Dynamically generating attribute values: Sequences

Often, we'd like to generate unique values for model attributes when new objects are created. For example, we may want the username/email of a user to be always unique. We can use sequences for this.

BackboneFactory.define_sequence('email', function(n){
    return "person"+n+"@example.com";
});

var email = BackboneFactory.next('email') // person1@example.com

Defining Factories with Sequences

BackboneFactory.define('user', User, function(){
    return {
        name : 'Backbone User',
        email: BackboneFactory.next('email')
    };
});
var user = BackboneFactory.create('user);
console.log(user.get('email') // person2@example.com

Contributing

  1. Open a ticket on GitHub, maybe someone else has the problem too
  2. Make a fork of my GitHub repository
  3. Make a spec driven change to the code base (we use jasmine)
  4. Make sure it works and all specs pass
  5. Update the README if needed to reflect your change / addition
  6. With all specs passing push your changes back to your fork
  7. Send me a pull request

License

(The MIT License)

Copyright (c) 2009, 2010, 2011

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.