我收到了一个包含少量测试用例的测试服,并使用NgRx进行了Angular 2应用程序示例。我无法通过一个测试用例,并且不确定该测试用例是否正确。
我的问题是NgRx store.dispatch
方法是否接受字符串作为参数?
如果没有,那么如何通过此测试用例?
如果是,您可以帮助我了解如何在这种情况下实施Action和reducer吗?
遇到麻烦的测试用例:
it('should dispatch action when dispatchAction is called', async() => {
// you need to spy on store's 'dispatch' method
store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough();
// if you call function dispatchAction with 'movies' paramter. expect store to dispatch action='movies'
component.dispatchAction('movies');
fixture.detectChanges();
expect(store.dispatch).toHaveBeenCalledWith('movies');
});
组件分派方法:
dispatchAction($event: string) {
// ToDo: need to write this.store.dispatch('movies') . But this method will not accept string parameter.
}
要调度movies
操作,只需使用操作界面的type
键:
dispatchAction($event: string) {
this.store.displatch({type: $event});
}