我想要测试 throwError 部分。OK 测试正常。我想测试是否使用getById
了错误的 id 0
,getById
返回一个错误 ( throwError
)
我的服务:
getById(fooId): Observable<Foo> {
return this.getAll().pipe(mergeMap(foos => {
const foo: Foo= foos.find(({id}) => id === fooId);
if (foo) {
return of(foo);
} else {
throwError('foo not found');
}
}
));
}
我的单元测试:
it('...', () => {
service.getById(0).subscribe(() => {
fail('expected error');
}, (error) => {
expect(error).toBe('foo not found');
});
const res= httpMock.expectOne({ url: '/api/admin/foo', method: 'GET' });
res.flush(
[
{
'id': 1,
'name': 'foo1'
},
{
'id': 2,
'name': 'foo2'
}
]);
});
我有这个错误:
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
我改变
throwError('Spada not found');
经过
return throwError('Spada not found');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句