简体   繁体   English

我如何不进行尝试就测试错误处理-catch

[英]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.

相关问题 如何强制 mongodb 错误以开玩笑的方式测试 try and catch 条件 - How can I force a mongodb error to test with jest the try and catch condition 如何在没有 try/catch 块的情况下使用异步 lambda 并且仍然有自定义错误消息? - How can I use async lambdas without a try/catch block and still have custom error messages? 如何从 Jest 中的 try/catch 块检查错误 - How I can check error from try/catch block in Jest 为什么Jest错误没有try / catch块进行异步测试 - Why does Jest error without try/catch blocks for async test try catch 与 promise 用于错误处理的链接机制哪一个提供更大的灵活性? - try catch vs promise chaining mechanism for error handing which one offers more flexibility? 我可以在不抛出异常的情况下打破 JS 中的 try-catch 吗? - Can I break a try - catch in JS without throwing exception? 如何使这个简单的Try / Catch测试工作? - How do I get this simple Try/Catch test to work? try catch 不能捕获错误 - Try catch can't catch error 在使用try / catch的Javascript中,除了错误消息外,如何捕获函数名称和所有传递的参数名称/值? - In Javascript using try / catch, besides the error message how can I catch function name and all arguments names/values passed? 我可以在 JavaScript 中使用 try/catch 而不指定 catch 参数/标识符吗? - Can I use a try/catch in JavaScript without specifying the catch argument/identifier?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM