Skip to content

Instantly share code, notes, and snippets.

@damncabbage
Created August 16, 2012 01:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save damncabbage/3365538 to your computer and use it in GitHub Desktop.
Save damncabbage/3365538 to your computer and use it in GitHub Desktop.
FactoryGirl-ish definitions in Jasmine
/*
* Set up factories, then create them in tests with (for example):
*
* LineItemFactory();
*
* Or with attributes / overrides:
*
* LineItemFactory({
* "id": 123,
* "order": OrderFactory({"firstName": "Example Associated Record Override"}),
* "quantity": 4
* });
*/
(function(){
function setUpFactory(type, defaultAttributes) {
return function(attributes) {
var defaults = (defaultAttributes instanceof Function ? defaultAttributes() : defaultAttributes);
var buildWithAttrs = _.extend({}, defaults, attributes);
return new type(buildWithAttrs);
}
}
//// Factories ////
window.LineItemFactory = setUpFactory(LineItem, function(){return{
'id': randomId(),
'order': OrderFactory(),
'name': 'Some Product Name',
'price': '14.99',
'quantity': 1,
'path': '/products/some-product-name',
'availability': 'Available now (shipped in 3-5 days)',
'formatName': 'DVD',
'imageUrl': 'http://s3.amazonaws.com/myexample/assets/products/123/thumb.jpg?12345',
'variantOptions': 'Colour: Blue'
}});
window.OrderFactory = setUpFactory(Order, function(){return{
'id': randomId(),
'number': 'E12345'
}});
//// Misc Helpers ////
function randomId() {
return Math.floor(Math.random() * 999999) + 1;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment