gpt4 book ai didi

ngrx 效果单元测试合并图中的多个 Action

转载 作者:行者123 更新时间:2023-12-04 02:49:20 24 4
gpt4 key购买 nike

我正在使用 ngrx 库并且有这样的效果

@Effect()
loadCollection$: Observable<Action> = this.actions$
.ofType(authAction.GET_USER)
.startWith(new authAction.GetUserAction()) // call on app load
.switchMap(() =>
this.service.getUser()
.mergeMap((user: User) => [
new authAction.GetUserCompleteAction(user),
new navigationAction.GetLinksCompleteAction(user.role)
])
);

我正在为它编写规范,它看起来像这样
 actions = new ReplaySubject(2);
actions.next(new auth.GetUserAction());

effects.loadCollection$.subscribe(result => {
expect(service.getUser).toHaveBeenCalled();
expect(result).toEqual(new navigation.GetLinksCompleteAction('test')); --> this line fails
});

我怎么能期望在合并映射中调用了多个操作。

最佳答案

您可以使用 jasmine-marbles测试条件,如 mergeMap .见 @ngrx/effects示例测试文档:https://github.com/ngrx/platform/blob/master/docs/effects/testing.md

在您的情况下,测试将如下所示:

  actions = hot('-a', { a: new authAction.GetUserAction() });

const expected = cold('-(bc)', { // the ( ) groups the values into the same timeframe
b: new authAction.GetUserCompleteAction({}), // put whatever mock value you have here
c: new navigationAction.GetLinksCompleteAction('test')
};

expect(effects.loadCollection$).toBeObservable(expected);

然后我将拆分测试以检查 expect(service.getUser).toHaveBeenCalled();成一个单独的测试用例。

https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.mdhot/cold句法。

关于ngrx 效果单元测试合并图中的多个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com