gpt4 book ai didi

angular - 如何对此效果进行单元测试(使用 {dispatch : false})?

转载 作者:太空狗 更新时间:2023-10-29 17:09:12 26 4
gpt4 key购买 nike

ngrx 和单元测试初学者在这里。我的效果如下:

@Injectable()
export class NotificationEffects {
@Effect({dispatch: false})
notificationShow$ = this.actions$
.ofType(notificationAction.NOTIFICATION_SHOW)
.do((action: notificationAction.NotificationShowAction) => {
this.notificationService.info(action.payload.config);
});

constructor(private actions$: Actions, private notificationService: NotificationService) {}
}

具体来说,我想测试是否已调用 notificationService 方法信息。我该怎么做?

我遵循了这些示例,但没有找到解决方案:

https://netbasal.com/unit-test-your-ngrx-effects-in-angular-1bf2142dd459 https://medium.com/@adrianfaciu/testing-ngrx-effects-3682cb5d760e https://github.com/ngrx/effects/blob/master/docs/testing.md

最佳答案

就这么简单:

describe('notificationShow$', () => {
let effects: NotificationEffects;
let service: any;
let actions$: Observable<Action>;
const payload = {test: 123};

beforeEach( () => {
TestBed.configureTestingModule( {
providers: [
NotificationEffects,
provideMockActions( () => actions$ ),
{
provide: NotificationService,
useValue: jasmine.createSpyObj('NotificationService', ['info'])
}
]
} );

effects = TestBed.get(NotificationEffects);
service = TestBed.get(NotificationService);
});

it('should call a notification service method info with a payload', () => {
actions$ = cold('a', { a: new notificationAction.NotificationShowAction(payload) });
effects.notificationShow$.subscribe(() => {
expect(service.info).toHaveBeenCalledWith(payload);
});
});
});

关于angular - 如何对此效果进行单元测试(使用 {dispatch : false})?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318095/

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