也许我不明白文档,我的测试不起作用。
我将测试函数中的预期错误:
// file_to_test.js
...
myFunctionToTest: function(param1, param2 = "AA-BB") {
const idx1 = param2.indexOf("AA");
if(idx1 === -1) {
throw new Error('test message error');
}
}
来了测试:
// test.js
sap.ui.define(["path/to/my/script"], function(myScriptToTest) {
...
QUnit.test('should thrown error', assert => {
assert.throws(
function() {
myScriptToTest.myFunctionToTest('blabla', 'blubblub'); // test is broken at this line
},
function(error) {
return error.toString() === 'test message error';
},
'Error thrown'
}
})
})
返回(浏览器-)输出:
1. Error thrown
Expected: function( a ){
[code]
}
Result: Error("test message error")
Diff: function( a ){
[code]
}Error("test message error")
Source: ...
如何正确设置测试?
您的 QUnit 代码看起来不错,但您编写 JavaScript 逻辑的方式存在错误。
事实上
error.toString()
不等于test message error
。您要找的是 error.message
而不是 error.toString()
。
考虑以下因素:
var myerror = new Error('hello world');
console.log(myerror.toString());
// 'Error: hello world'
console.log(myerror.toString() === 'hello world');
// false
console.log('-------');
console.log(myerror.message);
// 'hello world'
console.log(myerror.message === 'hello world');
// true
另请参阅: