未处理的拒绝错误。即使测试正在通过

时间:2016-12-20 01:00:56

标签: 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承诺,mongoose(promisified)和testdouble。顺便说一句,这是运行测试后来自控制台的日志:

dashBoard
Unhandled rejection Error: test-error
✓ handles errors

2 个答案:

答案 0 :(得分:2)

经过大量的反复试验后,我发现了正在发生的事情。该问题与testdouble API .thenReturn()有关。

如果.thenReturn(Promise.reject(...))我将td配置为使用Bluebird承诺并将其API用于承诺.thenReject(...),则会出现错误。结果测试代码为:

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);
});

此测试无需警告即可使用。

答案 1 :(得分:1)

我猜问题是

td.when(FB.getAsync(`731033223716085/events`))
.thenReturn(Promise.reject(mockError))

如果永远不会调用getAsync函数,并且从不使用被拒绝的promise(并且没有将错误处理程序链接到它)。您可以通过自己添加处理程序来告诉它忽略这一点:

function ignore(e) {}
function ignoredRejection(e) {
    var p = Promise.reject(e);
    p.catch(ignore);
    return p;
}

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