Re: testing mongoose validation with vows

131 views
Skip to first unread message

George Snelling

unread,
Nov 4, 2012, 1:48:38 AM11/4/12
to nod...@googlegroups.com
Try posting your question here:  https://groups.google.com/forum/?fromgroups=#!forum/mongoose-orm

-g 

On Saturday, November 3, 2012 9:39:31 AM UTC-7, Alberto Gori wrote:
I am trying to test validation on a very simple mongoose model, but I have some problem with exceptions. 

//model.js

var ResourceDef = new Schema({
name: {type: String, required: true},
content: String,
});

var Resource = mongoose.model('Resources', ResourceDef);

//test.js

vows.describe('test model behavior').addBatch({
'validating a Resource object with name equals to null': {
topic: function() {
new model.Resource({
name: null,
content: '.....',
}).validate(this.callback);
},
'should throw an error': function(e) {
assert.ok(e);
}
}
}).export(module);


I get this error:

    validating a Resource object with name equals to null 
      ✗ should throw an error 
        » An unexpected error was caught: ValidationError: Validator "required" failed for path name 

I don't understand why mongoose is throwing that ValidationError!

Alberto Gori

unread,
Nov 4, 2012, 6:07:59 AM11/4/12
to nod...@googlegroups.com
I discovered that the problem is not inherent to mongoose validation method, but to vows. Incredibly vows fails to manage Error in callback. For example:


'this model': {
    topic: function() {this.callback(new Error('error!!!'))}
    'should throw an error': function(e) {asser.ok(e);}
}

this very simple test fails with 

 » An unexpected error was caught: Error: error! 

I thought vows was a solid test library...what happened?

papandreou

unread,
Nov 5, 2012, 3:14:43 AM11/5/12
to nod...@googlegroups.com
It's actually a conscious design choice in vows, but I agree that it's a mistake. In the async case (last time I checked) it looks at the arity of your vow function (function.length), and if it takes less than two arguments, it assumes that you don't want the error passed to your function, just the "result", and that you want to classify it as an error every time an error is passed to this.callback.

I guess the underlying assumption is that an async function will always deliver a result to its callback, but it's clearly too magic. It has caused a lot of wtfs for me and my team. The "fix" is to add a bogus 2nd argument to your vow function.

Best regards,
Andreas Lind Petersen (papandreou)

Chad Engler

unread,
Nov 5, 2012, 9:03:17 AM11/5/12
to nod...@googlegroups.com

I was just having this exact issue the other day, thanks for the catch Papandreou! You just saved me some serious time.

 

-Chad

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Alberto Gori

unread,
Nov 7, 2012, 3:11:49 PM11/7/12
to nod...@googlegroups.com
I discovered that Vows has a not documented feature that completely solves this problem:

vows.describe('...

}).export(module, { error: false });

{error: false} is the answer!

Alberto Gori

unread,
Nov 7, 2012, 3:12:18 PM11/7/12
to nod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages