Skip to content
Browse files

Merge pull request #4 from szimek/factory_create_fix

Return saved object from Factory.create method
  • Loading branch information...
2 parents 2b41a84 + 4e7eb2c commit 40c471884b186ce71ddb7cc409f9e167face89e4 @cadwallion cadwallion committed Mar 23, 2012
Showing with 20 additions and 7 deletions.
  1. +3 −3 index.js
  2. +14 −3 spec/factory_spec.js
  3. +3 −1 spec/spec_helper.js
View
6 index.js
@@ -73,11 +73,11 @@ module.exports = {
data = {};
}
- this.build(model, data, function(err, obj){
+ this.build(model, data, function(err, builtObj){
if (err)
throw(err);
- obj.save(function(error) {
- callback(error, obj);
+ builtObj.save(function(error, createdObj) {
+ callback(error, createdObj);
})
});
}
View
17 spec/factory_spec.js
@@ -38,14 +38,14 @@ describe('Factory#build', function() {
})
it('uses the stored definition to seed attribute values', function() {
- var object = Factory.build('test')
+ var object = Factory.build('test')
expect(object.name).toEqual('Test Model')
expect(object.real).toEqual(false)
expect(object.dynamic).toEqual("dynamic")
})
it('takes an optional parameter to override stores attributes', function() {
- var object = Factory.build('test', { real: true })
+ var object = Factory.build('test', { real: true })
expect(object.name).toEqual('Test Model')
expect(object.real).toEqual(true)
})
@@ -95,7 +95,7 @@ describe('Factory#create', function() {
it('should override attributes like Factory#build', function() {
var object;
Factory.create('test', { name: function(){return 'Test Object';}, dynamic: "another value", called: function(cbk){return cbk(null, 'new value');} }, function(e, o) {
- object = o;
+ object = o;
})
waitsFor(function() { return object != null}, 'object creation failed', 1000);
runs(function() {
@@ -104,4 +104,15 @@ describe('Factory#create', function() {
expect(object.called).toEqual('new value');
})
})
+
+ it('should returned saved object', function() {
+ var object;
+ Factory.create('test', function(e, o) {
+ object = o;
+ })
+ waitsFor(function() { return object != null}, 'object creation failed', 1000);
+ runs(function() {
+ expect(object.isNew).toBe(false);
+ })
+ })
})
View
4 spec/spec_helper.js
@@ -1,10 +1,12 @@
Factory = require('../')
TestModel = function TestModel(attributes) {
+ this.isNew = true;
this.name = attributes.name;
this.real = attributes.real;
this.dynamic = attributes.dynamic;
this.called = attributes.called;
}
TestModel.prototype.save = function(callback) {
- callback(null)
+ this.isNew = false;
+ callback(null, this);
}

0 comments on commit 40c4718

Please sign in to comment.
Something went wrong with that request. Please try again.