我应该如何测试可观察对象抛出错误?

时间:2019-04-29 17:03:05

标签: javascript unit-testing error-handling jasmine observable

我应该如何测试可观察对象抛出错误?

const target = () => 
  Observable.of(1)
    .map(() => Observable.throw('an error'));

我尝试了以下方法来检查Jasmine的错误:

expect(target().subscribe(x => x)).toThrow();

let failingFunction = () => target;
expect(() => failingFunction).toThrow();

target.subscribe(() => null, err => expect(err).toBeDefined());

1 个答案:

答案 0 :(得分:-1)

你能做这样的事吗?

it('should throw an error', (done) => {
  target()
    .next(fail)
    .catch((err) => {
      expect(err).toBe('an error');
      done();
    });
});