预期对象是一种 ScalarObservable,但它是 Observable
Expected object to be a kind of ScalarObservable, but was Observable
在我的 Angular 应用程序中,我通过以下方式使用 Observables:
getItem(id): Observable<Object> {
return this.myApi.myMethod(...); // returns an Observable
}
我对它进行了单元测试:
it('getItem(...) should correctly do its job',
inject([MyService], (service: MyService) => {
const spy = spyOn(myApi, 'myMethod').and.returnValue(mockObservable);
const mockObservable = Observable.of('mock');
expect(service.getItem(123)).toEqual(mockObservable);
expect(spy).toHaveBeenCalledWith(...);
})
);
而且效果非常好。
但是,如果我尝试使用 catch
将 添加到我的 getItem()
方法中:
getItem(id): Observable<Object> {
return this.myApi.myMethod(...).catch(e => {
/* display a notification error ... */
return Observable.throw(e);
});
}
它仍然可以正常工作,但现在测试失败了,说:
Expected object to be a kind of ScalarObservable, but was Observable({ _isScalar: false, source: ScalarObservable({ _isScalar: true, value: 'mock', scheduler: null }), operator: CatchOperator({ selector: Function, caught: }) }).
这是什么意思?为什么会这样?我该如何解决这个问题?
感谢@Pace 的输入:
expect(service.getItem(123)).toEqual(mockObservable)
fails because
they are not the same object. The service method is returnning a new
observable that wraps mockObservable and so the check fails. Your test
would be better off subscribing to the observable returned from your
service call and ensuring it returns 'mock'
我找到了解决方案:
it('getItem(...) should correctly do its job',
inject([MyService], (service: MyService) => {
const spy = spyOn(myApi, 'myMethod').and.returnValue(mockObservable);
const mockData = 'mock'; // can be anything
const mockObservable = Observable.of(mockData);
service.getItems().subscribe((data) => {
expect(data).toEqual(mockData);
});
expect(spy).toHaveBeenCalledWith(...);
})
);
在我的 Angular 应用程序中,我通过以下方式使用 Observables:
getItem(id): Observable<Object> {
return this.myApi.myMethod(...); // returns an Observable
}
我对它进行了单元测试:
it('getItem(...) should correctly do its job',
inject([MyService], (service: MyService) => {
const spy = spyOn(myApi, 'myMethod').and.returnValue(mockObservable);
const mockObservable = Observable.of('mock');
expect(service.getItem(123)).toEqual(mockObservable);
expect(spy).toHaveBeenCalledWith(...);
})
);
而且效果非常好。
但是,如果我尝试使用 catch
将 getItem()
方法中:
getItem(id): Observable<Object> {
return this.myApi.myMethod(...).catch(e => {
/* display a notification error ... */
return Observable.throw(e);
});
}
它仍然可以正常工作,但现在测试失败了,说:
Expected object to be a kind of ScalarObservable, but was Observable({ _isScalar: false, source: ScalarObservable({ _isScalar: true, value: 'mock', scheduler: null }), operator: CatchOperator({ selector: Function, caught: }) }).
这是什么意思?为什么会这样?我该如何解决这个问题?
感谢@Pace 的输入:
expect(service.getItem(123)).toEqual(mockObservable)
fails because they are not the same object. The service method is returnning a new observable that wraps mockObservable and so the check fails. Your test would be better off subscribing to the observable returned from your service call and ensuring it returns'mock'
我找到了解决方案:
it('getItem(...) should correctly do its job',
inject([MyService], (service: MyService) => {
const spy = spyOn(myApi, 'myMethod').and.returnValue(mockObservable);
const mockData = 'mock'; // can be anything
const mockObservable = Observable.of(mockData);
service.getItems().subscribe((data) => {
expect(data).toEqual(mockData);
});
expect(spy).toHaveBeenCalledWith(...);
})
);