【发布时间】: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.
每次测试的存根都抛出错误有点烦人,我找不到解决这个问题的方法。 有没有办法做到这一点?
【问题讨论】:
-
使用 catch developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 或尝试捕获块。
标签: javascript node.js mocha.js chai sinon