誓言JS测试未定义
Vows JS Testing for Undefined
我正在尝试使用誓言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);
这不是确切的代码,但这是它的要点。当我运行测试时,我会得到"callback not fired"。通过誓言的代码,我可以看到它在主题为undefined
时分支。
最终,我想知道如何编写单元测试来做到这一点。我团队中的其他人写了我认为是黑客的东西,并在主题中进行了断言,如果是topic === undefined
,则返回true
或false
。
来自Vows文档:
»主题是一个可以执行异步代码的值或函数。
在您的示例中,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);
您可以提供这样的回调:观察具有**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);
相关文章:
- AngularJS指令单元测试中未定义的函数
- 未捕获的ReferenceError:未定义测试(函数)
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 类型错误:无法读取未定义的属性“apply”,在测试中使用 javascript apply
- 引用错误:未定义测试
- 测试Ember.JS应用程序失败,返回ReferenceError:未定义Ember
- AngluarJS单元测试-未定义不是函数
- AngularJS Jasmine测试:类型错误:'未定义'不是对象
- 因果报应单元测试'未定义'不是函数错误
- 未定义的Es6级在单元测试中与大口摩卡
- 如何进行单元测试以查看角度是否未定义
- 因果报应测试中未定义的预期范围变量
- "未定义“角度”;在Liferay中存储的角度应用程序上执行Protractor测试时出错
- 在javascript中测试变量是否为null,而不会导致未定义的错误
- TypeError:无法读取属性'那么'在使用Karma测试AngularJS控制器时未定义的
- 茉莉花测试显示模块未定义
- 无法在茉莉花测试中调用$scope.form.$submitted:未定义
- 当使用 Jasmine 对我的 angularJS 应用程序进行单元测试时,$interval是未定义的
- 如何测试包含未定义元素的数组的相等性
- 未捕获引用错误ccifi未定义测试伙伴:424 onclick