[英]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.