"TypeError:无法调用方法'投掷'未定义的“;单元内测试
"TypeError: Cannot call method 'throws' of undefined" in unit test
我想在单元测试中创建假错误。这是我的测试科目:
/arn.js
var fs = require('fs');
var filename = ['README.md', 'ioio.txt', 'yoyo.txt','passwd'];
exports.readFile = function(entry,callback){
var cntent = fs.readFile(entry, "utf8" ,function(err, data) {
if (err) throw err;
if (callback && typeof(callback) === "function") {
callback(null,data.substring(0,5));
}
});
}
/test/m1.js
var mock = require('mock')
var realB = require("../arn.js")
var test = require('unit.js');
var trigger1 = function(){
throw new Error ("File not found in my computer",null);
};
var b = mock("../arn.js", {
fs: {
readFile: function (entry,encoding, callback) {
if (entry === "cody.txt"){
var regEx = /txt$/;
if(regEx.test(entry)){
callback(null,'text2');
}
}
if (entry === "notfound.sure"){
callback(trigger1);
}
}
}
}, require);
describe('Test with static input', function(){
it('should return text2', function(done){
b.readFile('cody.txt', function(err,a){
test.value(a).match('text2');
done();
});
});
it('should return Invalid file extension', function(done){
b.readFile('notfound.sure', function(err, a){
test.error.hasMessage('File not found in my computer');
done();
});
});
it('should return not found', function(done){
test.asswert.throws(
function(){
b.readFile('notfound.sure');
},
function(err){
if ( (err instanceof Error)){
return true;
}
},
"Expected error"
);
done();
});
});
输出:
Test with static input
✓ should return text2
1) should return Invalid file extension
2) should return not found
1 passing (8ms)
2 failing
1) Test with static input should return Invalid file extension:
Error: the function [Function] was thrown, throw an Error :)
2) Test with static input should return not found:
TypeError: Cannot call method 'throws' of undefined
at Context.<anonymous> (test/m1.js:46:16)
我如何为他们找到解决方案?这个专业术语让我绕了好几个小时。
将行test.asswert.throws
更改为test.assert.throws
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- AngularJS指令单元测试中未定义的函数
- AngluarJS单元测试-未定义不是函数
- 因果报应单元测试'未定义'不是函数错误
- 未定义的Es6级在单元测试中与大口摩卡
- 如何进行单元测试以查看角度是否未定义
- 当使用 Jasmine 对我的 angularJS 应用程序进行单元测试时,$interval是未定义的
- 通过 API 将 JSON 拉入 Google 表格,导致单元格中出现未定义的值
- 类型错误:parsed 在 angularjs 服务单元测试中未定义
- 当不处于编辑模式时,下拉列表中的Kendo单元格值显示为未定义
- Angular2单元测试Http MockBackend问题-无法读取mergeOptions中未定义的属性合并
- 无法读取属性'0'的未定义(仅在单元测试中)
- Ext.JS 4.2.1使用Jasmine进行单元测试-未定义应用程序
- 美元的范围.单元测试期间控制器中未定义的$on
- 表格单元格中预填充的文本显示为“未定义”.当在JS中添加HTML标签时
- 未定义不是函数:单元测试护照身份验证自定义回调
- JQuery中,html()对表格单元格返回“未定义”,而不是“函数”
- "TypeError:无法调用方法'投掷'未定义的“;单元内测试