[英]How to write a marble test for a combineLatest Observable?
我有一個 Observable,它接受兩個Observable<boolean>
並使用combineLatest
對它們運行“或”操作。
interface LoadingEventEmitter {
isLoading$: Observable<boolean>
}
export class LoadingService {
isLoading$: Observable<boolean>;
constructor(
private requests: LoadingEventEmitter,
private lazyRoutes: LoadingEventEmitter
) {
this.isLoading$ = combineLatest([
this.requests.isLoading$,
this.lazyRoutes.isLoading$,
]).pipe(
map(
([requestsLoading, lazyRoutesLoading]) =>
requestsLoading || lazyRoutesLoading
)
);
}
}
我正在嘗試使用jasmine-marbles
對其進行測試。
fdescribe('LoadingService', () => {
const createLoadingService = (
requests$: Observable<boolean>,
lazyLoading$: Observable<boolean>
) => {
const mockRequests = { isLoading$: requests$ };
const mockLazyLoading = { isLoading$: lazyLoading$ };
return new LoadingService(mockRequests, mockLazyLoading);
};
const values = { t: true, f: false };
it('isLoading$ should be true when at least one of the sources emits true', () => {
const a = cold('---t---f---f---', values); // First source
const b = cold('f----t---------', values); // Second source
const c = cold('---t-t-t---t---', values); // Result
const service = createLoadingService(a, b);
expect(service.isLoading$).toBeObservable(c);
});
});
測試對我來說看起來很好,但它失敗並出現以下錯誤:
Expected $[0].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[1].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[2].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[3].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
該錯誤是什么意思,我該如何解決?
我設法用rxjs-marbles
做到了:
it(
'isLoading$ should be true when at least one of the sources emits true',
marbles((m) => {
const a = m.cold('---t---f---f---', values);
const b = m.cold('f----t---------', values);
const c = ' ---t-t-t---t---';
const service = createLoadingService(a, b);
m.expect(service.isLoading$).toBeObservable(c, values);
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.