Question

I'm trying to start my node.js project with BDD using vows. Then I got this strange error.

I was trying to write a small route testing for express with vows and here is my original code,

node_server.prototype.mainpage = function(callback) {
  http.get({host:'localhost', port:this.port, path:'/', agent:false}, function(res){
  callback(res.statusCode);
  });
}

And here is how I write my vows testing

vows.describe('Request to the server').addBatch({
'Should get http 200': {
      topic: function () {
        app_server.mainpage(this.callback)
      },

      'we get 200': function (statusCode) {
        app_server.close_server();
        assert.equal(statusCode, 200);
      }
  },
}).run(); // Run it

Vows always report an unexpected error even though the statusCode is correct, like this

» An unexpected error was caught: 200

So I change my mainpage function like this

node_server.prototype.mainpage = function(callback) {
  http.get({host:'localhost', port:this.port, path:'/', agent:false}, function(res){
      callback("error", res.statusCode); // Here I added a err message in front of the status code.
  });
}

And I also change my test suite as

'we get 200': function (err, statusCode) {

Then this testing worked! I'm just wondering how could this strange situation happened. I've read the documentation of vows but I didn't find anywhere they said I must put 2 parameters to a cb instead of 1.

Give me some clues! Thank you in advance!

Était-ce utile?

La solution

The Vows documentation doesn't mention it, but it's assuming the error first callback pattern, which is widely adopted by the Node.js community. In this pattern, the first parameter of the callback is always an error. If it's null or undefined, then you know the async operation succeeded. I would recommend using this pattern for all of your own code as well, even if the function won't ever produce an error parameter.

If you use this pattern yourself, you can more easily make use of other libraries like async, which can make things a lot easier for complicated apps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top