使用$ q.reject可能无法处理拒绝

时间:2017-05-08 18:25:02

标签: angularjs jasmine es6-promise

我正在使用Angular和jasmine并得到错误:

Possibly unhandled rejection: {"body2":{}} thrown

我的代码抛出错误:

createAccount() {
  // Return some ES6 Promise/async call with return value from test.
  return this.request({method: 'POST', body: {}})
      .catch(error => {
        if (error.flaky) {
            // Handle a specific error with another call
            return this.createDifferentAccount();
        }

        console.log('Passed the specific case');
        // Generically fail for other errors.
        this.$q_.reject(error);
      });
}

我的茉莉花测试案例(失败):

it('returns other errors', () => {
  const outerError = {
    body2: {},
  };
  spyOn(service, 'request')
      .and.returnValues($q.reject(outerError));

  const result = service.createAccount()
      .catch(() => {
        console.log('This is never called');
      });
  expect(result).toHaveBeenRejected();
  console.log('Neither is this');
});

如何处理此错误/我做错了什么?

1 个答案:

答案 0 :(得分:2)

您只是拒绝错误,而不是拒绝拒绝。

修复很简单,更改

this.$q_.reject(error);

return this.$q_.reject(error);

请记住始终从.then()或.catch()块中返回值!