2013-03-05 80 views
0

出于某种原因,我似乎无法获得vows.js子主题在我的真实测试套件中工作,但它们在示例文件中工作正常......可以发现我的问题吗?我使用vows.js子主题有什么问题?

这工作:

node tests/learning-vows.js 

我得到:

vows.describe('An Education in Vows').addBatch({ 
    'when I write an asynchronous topic': { 
     topic: function() { 
      var that = this; 
      setTimeout(function() { 
       that.callback(true); 
      }, 100); 
     }, 
     'it passes': function (topic) { 
      assert.equal(topic, true); 
     }, 
     'and it has an asynchronous sub-topic': { 
      topic: function() { 
       var that = this; 
       setTimeout(function() { 
        that.callback(true); 
       }, 100); 
      }, 
      'it also passes': function (topic) { 
       assert.equal(topic, true); 
      } 
     } 
    } 
}).run(); 

当我通过运行这个

· 
· 
✓ OK » 2 honored (0.207s) 

这不起作用:

我有一个文件./tests/smoke.js

vows.describe('Registration & Authentication').addBatch({ 
    'when a user registers with a valid username and password': { 
     topic: function() { 
      client.register({ 
       username: validusername, 
       password: validpassword 
      }, this.callback); 
     }, 
     'we return status 200 OK': function (data, response) { 
      assert.equal(200, response.statusCode); 
     }, 
     'simple sub-topic': { 
      topic: true, 
      'should work': function(topic) { 
       assert.equal(true, topic); 
      } 
     }, 
    } 
}).run() 

当我通过执行此:

node tests/smoke.js 

我得到:

· 
✗ Errored » 1 honored ∙ 1 errored 

注意的是,在第二个例子中,没有子话题,我得到:

· 
✓ OK » 1 honored (0.100s) 
+0

你尝试和'console.log'你上次'assert.equal'就是测试作为话题? – Floby 2013-03-05 15:01:47

+0

如果不是'topic:true,'你做了'topic:function(){return true; }' – Pointy 2013-03-05 15:02:22

回答

0

Vows正在使用节点的回调约定(请参阅:http://nodemanual.org/latest/nodejs_dev_guide/working_with_callbacks.html),它假定回调中的第一个参数是一个错误对象。

因此当您发送data作为第一个参数时,您会发誓在client.register中发生错误。它可以防止誓言评估子话题。子主题被标记为错误,但断言成功并且当前主题被标记为已兑现。

从输出中猜测它确实不是微不足道的。此外誓言的行为是不一致的,请尝试替换true0,然后'0'作为第一次测试中的回调参数,您将看到另外两个结果。

这里是一个工作为例:

var vows = require('vows'), assert = require('assert'); 

var client = { 
    register: function(obj,callback){ 
    callback(null, obj, {statusCode:200}); 
    } 
}; 
vows.describe('Registration & Authentication').addBatch({ 
    'when a user registers with a valid username and password': { 
     topic: function() { 
      client.register({ 
       username: 'validusername', 
       password: 'validpassword' 
      }, this.callback); 
     }, 
     'we return status 200 OK': function (err, data, response) { 
      assert.equal(response.statusCode, 200); 
     }, 
     'simple sub-topic': { 
      topic: true, 
      'should work': function(topic) { 
       assert.equal(true, topic); 
      } 
     } 
    } 
}).export(module)