2013-01-08 23 views
0

我正在尝试使用誓言js创建单元测试。当“主题”未定义时,我遇到了麻烦。请看下面的例子:未定义的誓言JS测试

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

function giveMeUndefined(){ 
    return undefined; 
} 

vows.describe('Test vow').addBatch({ 
    'When the topic is undefined': { 
    topic: function() { 
     return giveMeUndefined(); 
    }, 
    'should return the default value of undefined.': function(topic) { 
     assert.isUndefined(topic); 
    } 
    } 
}).export(module); 

这不完全是代码,但它是它的要点。当我运行测试时,我得到“回调未被解雇”。通过誓言的代码,当主题为undefined时,我可以看到它的分支。

最终我想知道如何编写单元测试来做到这一点。我团队中的其他人写了我认为是黑客行为并做了主题断言并返回truefalse如果topic === undefined

回答

0

从誓言文档:

»主题是一个值或能够执行异步代码的功能。

在您的示例topic被分配给一个函数,所以誓言期待异步代码。

只需重写你的题目如下:

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

function giveMeUndefined(){ 
    return undefined; 
} 

vows.describe('Test vow').addBatch({ 
    'When the topic is undefined': { 
    topic: giveMeUndefined(), 
    'should return the default value of undefined.': function(topic) { 
     assert.isUndefined(topic); 
    } 
    } 
}).export(module); 
0

你可以提供一个回调是这样的:观察与**Note**

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

function giveMeUndefined(callback){//**Note** 
    callback(undefined); //**Note** 
} 

vows.describe('Test vow').addBatch({ 
    'When the topic is undefined': { 
    topic: function(){ 
    giveMeUndefined(this.callback); // **Note** 
    }, 
    'should return the default value of undefined.': function(undefinedVar, ignore) { 
     assert.isUndefined(undefinedVar); 
    } 
    } 
}).export(module); 
线