Los*_*san 6 jasmine rxjs karma-jasmine ngrx-effects angular
我有一个效果,在一种情况下返回一个EMPTYObservable。我正在尝试测试这个案例,但我似乎不知道如何测试EMPTYObservable?我的代码如下:
效果:
effect$ = createEffect(() =>
this.actions$.pipe(
ofType(someAction),
mergeMap(([action]) => {
if (someCondition) {
return EMPTY <-- this is what I am trying to test
}
return someServiceCall.pipe(
map((offers) => //dispatch some action,
catchError((error: string) => // dispatch another action),
)
}),
),
)
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试尝试,但失败了
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)。
考虑到测试中已经满足条件:
it('should return EMPTY when ...', (done) => {
actions$ = of(someAction)
effects.effect$.subscribe((res) => {
expect(res).toEqual(never)
done()
})
})
Run Code Online (Sandbox Code Playgroud)
of(null)如果返回的是而不是,我可以让它工作EMPTY。但我真的很想知道如何测试这个案例。
Pan*_*kos 11
看来我们在 RxJs 中有一个特定的操作来检查可观察量是否为空。操作是isEmpty()
it('should return EMPTY when ...', (done) => {
actions$ = of(someAction)
effects.effect$.pipe(isEmpty()).subscribe( (res) => {
expect(res).toEqual(true)
done()
});
Run Code Online (Sandbox Code Playgroud)
仅当 observable 确实返回空时 res 才会为 true
| 归档时间: |
|
| 查看次数: |
11510 次 |
| 最近记录: |