javascript - 未处理的拒绝错误。即使测试通过

标签 javascript node.js asynchronous bluebird

我正在测试这个功能:

UserController.prototype.getDashboard = function getDashboard(req, res, next) {
  let pages, user;

  return this.User.findById(req.user.id)
    .populate('club')
    .execAsync()
    .then(dbUser => {
      user = dbUser;
      FB.setAccessToken(user.token);

      return FB.getAsync('me/accounts')
    })
    .then(fbPages => {
      pages = fbPages.data;

      return Promise.all(pages.map(page => {
        return FB.getAsync(`${page.id}/events`)
          .then(events => events)
          .catch(e => next(Boom.wrap(e)));
      }))
    })
    .then(events => {
      let entity = {
        user,
        pages: _.map(pages, (page, i) => _.assign({}, page, { events: events[i].data }))
      };

      return res.send(entity);
    })
    .catch(err => next(Boom.wrap(err)));
};

但是当我测试它时,即使它通过了,我也会得到 Unhandled rejection Error

这是我的测试:

 it('handles errors', (done) => {
  let mockError = new Error('test-error');
  let mockRequest = { user: { id: mockUser._id }};
  let mockNext = td.function();
  let capture = td.matchers.captor();

  td.replace(FB, 'getAsync');
  td.when(FB.getAsync('me/accounts'))
    .thenReturn(Promise.resolve(usersTestData.getDashboard.pages));

  td.when(FB.getAsync(`1769754093273789/events`))
    .thenReturn(Promise.resolve(usersTestData.getDashboard.events[0]))

  // Promise rejection was handled asynchronously
  td.when(FB.getAsync(`731033223716085/events`))
    .thenReturn(Promise.reject(mockError))

  td.when(mockNext(Boom.wrap(mockError)))
    .thenDo(() => { done() });

  controller.getDashboard(mockRequest, _.noop, mockNext);
});

奇怪的是,它通过了。因此,mockNext 正在按照我的预期进行调用,但我在控制台中收到了未处理拒绝的日志。我已尝试为映射的每个值处理 .catch,但它仍然显示相同的错误(警告?)。

我正在使用 Bluebird promises、mongoose(promisified) 和 testdouble。顺便说一句,这是运行测试后来自控制台的日志:

dashBoard
Unhandled rejection Error: test-error
✓ handles errors

最佳答案

经过大量试验和错误后,我发现发生了什么。问题与 testdouble API .thenReturn() 有关。

如果 .thenReturn(Promise.reject(...)) 出现错误,我将 td 配置为使用 Bluebird promises 并将其 API 用于 promises 。然后拒绝(...)。结果测试代码将是:

td.config({ promiseConstructor: require('bluebird') });

it('handles errors', (done) => {
  let mockError = new Error('test-error');
  let mockRequest = { user: { id: mockUser._id }};
  let mockNext = td.function();
  let capture = td.matchers.captor();

  td.replace(FB, 'getAsync');
  td.when(FB.getAsync('me/accounts'))
    .thenReturn(Promise.resolve(usersTestData.getDashboard.pages));

  td.when(FB.getAsync(`1769754093273789/events`))
    .thenResolve(usersTestData.getDashboard.events[0])

  td.when(FB.getAsync(`731033223716085/events`))
    .thenReject(mockError)

  td.when(mockNext(Boom.wrap(mockError)))
    .thenDo(() => { done() });

  controller.getDashboard(mockRequest, _.noop, mockNext);
});

此测试将在没有警告的情况下运行。

关于javascript - 未处理的拒绝错误。即使测试通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41233385/

相关文章:

javascript - 单击按钮时如何更改特定的img src?

javascript - 将谷歌表单响应行复制到新工作表中

javascript - 使用 Cognito 并为 AWS IOT 创建策略时,iot :ClientId policy variable? 的值是多少

javascript - Javascript 中的异步循环

swift - 无法将类型 'Promise<Void>' 的返回表达式转换为返回类型 'Promise<JSON>' Swift

javascript - 按两个不同的值过滤 JSON

javascript - TicTacToe 按钮继续以直线而不是网格显示

node.js - NodeJS STARTTLS 使用 SNI

node.js - 从客户端创建套接字 IO channel

c# - 澄清多线程/异步/并行编程