我正在编写验证文本的代码。这就是我测试的方式。但我不想在单元测试中使用try-catch。请给我一种更好的方法。
it('Testing if hook errors on invalid description.', async () => {
try {
workRequest.requestor = 'John';
workRequest.description = 1;
result = await app.service('dummy').create(workRequest);
} catch (error) {
assert.equal(error.errors.description, 'Description is must be a string');
}
});
答案 0 :(得分:3)
您要找的东西是这样的 应该扔 几乎所有测试框架都支持此API
例如:
应该
https://shouldjs.github.io/#should-throws
摩卡咖啡
还有
https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message
it('Testing if hook errors on invalid description.', async () => {
assert.throws( () => {
workRequest.requestor = 'John';
workRequest.description = 1;
result = await app.service('dummy').create(workRequest);
},
err
);