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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...