我正在使用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');
});
如何处理此错误/我做错了什么?
答案 0 :(得分:2)
您只是拒绝错误,而不是拒绝拒绝。
修复很简单,更改
this.$q_.reject(error);
到
return this.$q_.reject(error);
请记住始终从.then()或.catch()块中返回值!