【问题标题】:Silent Sinon stub error throwing in my Mocha test report在我的 Mocha 测试报告中抛出 Silent Sinon 存根错误
【发布时间】:2020-06-22 11:35:27
【问题描述】:

我想对存根函数抛出的每个Error 保持沉默。 例如这个代码块:

it(`should return a JSON object containing an error message and a status code of
            "${BAD_REQUEST}" if the request was unsuccessful.`, (done) => {

    const errMsg = 'Could not fetch users.';

    sandbox.stub(UserDao.prototype, 'getAll').throws(new Error(errMsg));

    agent.get(getUsersPath)
          .end((err: Error, res: Response) => {
              expect(res.status).to.equal(BAD_REQUEST);
              expect(res.body.error)
              expect(res.body.error).to.equal(errMsg);
              done();
          });
    });

正在给我以下输出:

User Routes
    "GET:/api/users/all"
      ✓ should return a JSON object with all the users and a status code of "200" if the
            request was successful.
Error: Could not fetch users.
    at Context.it (/Users/qeude/dev/categories-game-api/tests/Users.spec.ts:67:62)
    at callFnAsync (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runnable.js:385:21)
    at Test.Runnable.run (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runnable.js:329:7)
    at Runner.runTest (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:625:10)
    at /Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:749:12
    at next (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:542:14)
    at /Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:552:7
    at next (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:453:14)
    at Immediate.<anonymous> (/Users/qeude/dev/categories-game-api/node_modules/mocha/lib/runner.js:520:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
      ✓ should return a JSON object containing an error message and a status code of
            "400" if the request was unsuccessful.

每次测试的存根都抛出错误有点烦人,我找不到解决这个问题的方法。 有没有办法做到这一点?

【问题讨论】:

标签: javascript node.js mocha.js chai sinon


【解决方案1】:

假设您的生产代码正在使用一些只记录所有错误的记录器。可能只是console.error

在不同的项目和情况下,我采用了两种策略:

  • successfull 测试的所有错误静音(因为对于失败,您可能对任何关于错误的提示感兴趣)
  • 存根记录器并期望这个错误真的被捕获和记录

所以,mocha BDD 中的第一个解决方案可能如下所示:

// somewhere in global test module and/or offending suite
beforeEach(() => {
    TheGlobalLogger.redirectToBuffer();
});
afterEach(() => {
    if (this.currentTest.state !== "passed") {
        console.log("--- dumping logs from failed test ---");
        TheGlobalLogger.dumpBufferedLogs();
    }
    TheGlobalLogger.disableRedirectToBuffer()
});

当然,TheGlobalLogger 可以使用任何方法来捕获console 日志或您使用的任何其他机制。

您还可以通过对日志的期望来装饰特定的流程。其他项目的示例:

   it("...", () => {
     assertWillLogSync(() => {
       // some code that ultimately provoke console.warn with particular message
       console.warn("error: fooBar"),
     },
     console, "warn", /error: fooBar/
   );

在您的情况下,您需要 async 版本。 assertWillLogSync 实现示例。

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多