Created
August 16, 2012 01:52
-
-
Save damncabbage/3365538 to your computer and use it in GitHub Desktop.
FactoryGirl-ish definitions in Jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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