NGXS分派操作后如何测试服务方法toHaveBeenCalled

问题描述

我尝试运行测试,但是每次测试都会失败。我做错了

1。我的商店操作

@Action(GetDataAction)
  getDataList(ctx: StateContext<any>) {
    const state = ctx.getState();
    return this.service.getData()...

2。添加初始化变量。 在这里,我正在测试用例中创建一个服务变量。据我了解,我的服务将成为所有人的服务。


    describe('Store',() => {
      let actions$: Observable<any>;
      let store: Store;
      let service: SomeService;
    
      beforeEach(() => {
        Testbed.configureTestingModule({
          imports: [HttpClientModule,NgxsModule.forRoot([AppStore])],});
        store = Testbed.inject(Store);
        actions$ = Testbed.inject(Actions);
        service = new SomeService(null);
      });

3。添加测试

  it(should be run service.getData() after action new GetDataAction()',(done) => {

    const spy = spyOn(service,'getData').and.callFake(() => {
      return EMPTY;
    });

    store.dispatch(new GetDataAction()).subscribe(() => {
      expect(spy).toHaveBeenCalled();
      done();
    });
  });

Error text: Store > should be run service.getData() after action new GetDataAction() Expected spy getData to have been called. Error: Expected spy getData to have been called. at <Jasmine> at SafeSubscriber._next (http://localhost:9876/_karma_webpack_/src/app/store/store.spec.ts:41:19) at SafeSubscriber.__tryOrUnsub (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:183:1) at SafeSubscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:122:1)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)