[英]How can I test error handing without try— catch
I am writing code that validates texts. 我正在编写验证文本的代码。 This is how I test it.
这就是我测试的方式。 But I don't want to use try--catch in unit testing.
但我不想在单元测试中使用try-catch。 Please give me a better way to do it.
请给我一个更好的方法。
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');
}
});
what you looking for is somthing like this should.throws almost all tests framework supports this API 您正在寻找的东西应该像这样。 抛出几乎所有测试框架都支持此API
for example : 例如 :
shouldjs shouldjs
https://shouldjs.github.io/#should-throws https://shouldjs.github.io/#should-throws
mocha 摩卡
https://mochajs.org/#bdd https://mochajs.org/#bdd
and also 并且
https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message 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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.