我在Mocha中有以下测试用例..
.toThrowError("invalid")
然而,尽管下面的代码(if块中的代码正确执行)
try {
let arr = [];
existing.forEach((exists) => {
arr.push(...this.dependencies[exists]);
});
for (let x in arr){
if(existing.indexOf(arr[x]) == -1){
throw new Error('invalid');
}
}
}
catch(e){
console.log(e);
}
我收到以下消息。我在这里做错了什么?
Expected the function to throw an error matching:
"Invalid Base Permissions"
But it didn't throw anything.
答案 0 :(得分:2)
你自己发现了这个错误,所以就mocha而言,它没有抛出错误(错误在它进入测试之前得到处理)。如果您不依赖于代码中捕获的错误,请删除try-catch,它应该(假设实际上已经抛出错误)工作。