node.js - 使用 should.js/Mocha 捕获错误
问题描述
我目前正在使用 Mocha 和断言库 should.js
我试图在我的单元测试中遇到一种情况,它会引发异常 - 但从查看文档来看,到目前为止我没有太多运气让它工作。
以下代码块目前正在使用:
it('Adds a new employee to the db - FAILS', funct
let employeeObj = {
"Title": "Mr",
"FirstName": "Keanu ",
"LastName": "Reeves",
"Username": "KeanuReeves2",
"Password": "Password",
"Email": "keanu@reeves.com",
"IsActive": true
};
should(function () {
db.AddNewEmployee(employeeObj);
}).throw("U wot m8");
done();
});
我不断收到错误:
AssertionError: expected Function { name: '' } to throw exception
at Assertion.fail (node_modules\should\cjs\should.js:275:17)
at Assertion.value (node_modules\should\cjs\should.js:356:19)
at Context.<anonymous> (common\spec\knexDBServiceSpec.js:213:25)
有没有人遇到过这个问题或者能给我一些关于哪里出错的指导?
解决方案
它完全按照您的设置进行操作。
你告诉应该,你的函数应该抛出一个异常“U wot m8”。但它没有,因此应该失败。
期望(应该)是它应该抛出。