Javascript 誓言JS测试未定义
我尝试使用vows js创建单元测试。当“主题”为“未定义”时,我遇到了麻烦。请参见下面的示例:Javascript 誓言JS测试未定义,javascript,node.js,unit-testing,vows,Javascript,Node.js,Unit Testing,Vows,我尝试使用vows js创建单元测试。当“主题”为“未定义”时,我遇到了麻烦。请参见下面的示例: var vows = require('vows'), assert = require('assert'); function giveMeUndefined(){ return undefined; } vows.describe('Test vow').addBatch({ 'When the topic is undefined': { topic: function
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);
这不是确切的代码,但它是它的要点。当我运行测试时,我得到了“callbacknotfired.”逐步浏览誓言代码,我可以看到当主题为未定义时,它会分支
最终,我想知道如何编写单元测试来实现这一点。我团队中的其他人写了我认为是黑客的东西,并在主题中做了断言,并返回了<代码>真的< /代码>或<代码> false <代码> > <代码>主题==未定义< /代码> .< /p> 从DOCs:
»主题是可以执行异步代码的值或函数
在您的示例中,topic
被分配给一个函数,所以vows需要异步代码
只需按照以下方式改写您的主题:
var-vows=require('vows'),
assert=require('assert');
函数giveMeUndefined(){
返回未定义;
}
誓言。描述(“测试誓言”)。addBatch({
“主题未定义时”:{
主题:giveMeUndefined(),
“应返回未定义的默认值。”:函数(主题){
assert.isUndefined(主题);
}
}
}).出口(模块);
您可以提供如下回调:使用**注意**
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);