可以在带有NgRx存储的Angular 2中使用字符串参数调用store.dispatch方法

问题描述 投票:0回答:1

我收到了一个包含少量测试用例的测试服,并使用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.
}
angular karma-jasmine ngrx ngrx-store
1个回答
0
投票

要调度movies操作,只需使用操作界面的type键:

dispatchAction($event: string) {   
  this.store.displatch({type: $event});
}
© www.soinside.com 2019 - 2024. All rights reserved.