4

我有一个 nrwl/nx 工作区,其中有一个名为missions-sharedThis library has a feature state,在密钥下missions我还有一个后端服务,名为BackendServiceClient. 此后端返回承诺(由于代码生成)

我的问题是后端返回承诺,而 jasmine-marbles 无法识别:

我正在尝试模拟后端。我为此使用了一个测试提供者


let backend = {
  get: () => Promise.resolve({items: []})
}

TestBed.configureTestingModule({
...
providers : [{
  provide: BackendServiceClient, useValue: backend
}]
...

这是 MissionEffect :


@Injectable()
export class MissionsEffects {
  @Effect() loadMissions$ = this.dataPersistence.fetch(
    MissionsActionTypes.LoadMissions,
    {
      run: (action: LoadMissions, state: MissionsPartialState) => {
        return from(this.backend.get(new GetMissions())).pipe(
          map(r => new MissionsLoaded(r.items))
        );
      },

      onError: (action: LoadMissions, error) => {
        console.error('Error', error);
        return new MissionsLoadError(error);
      }
    }
  );

  constructor(
    private dataPersistence: DataPersistence<MissionsPartialState>,
    private backend: BackendClientService
  ) {}
}

然后我尝试使用 jasmine-marbles 测试这些@Effect

   actions = hot('-a-|', { a: new LoadMissions() });
   expect(effects.loadMissions$).toBeObservable(
     hot('-a-|', { a: new MissionsLoaded([]) })
   );

但是我在测试中遇到了这个错误:

  ● MissionsEffects › loadMissions$ › should work

    expect(received).toEqual(expected) // deep equality

    - Expected
    + Received

    - Array [
    -   Object {
    -     "frame": 10,
    -     "notification": Notification {
    -       "error": undefined,
    -       "hasValue": true,
    -       "kind": "N",
    -       "value": MissionsLoaded {
    -         "payload": Array [],
    -         "type": "[Missions] Missions Loaded",
    -       },
    -     },
    -   },
    -   Object {
    -     "frame": 30,
    -     "notification": Notification {
    -       "error": undefined,
    -       "hasValue": false,
    -       "kind": "C",
    -       "value": undefined,
    -     },
    -   },
    - ]
    + Array []

似乎是因为我正在使用 Promise 模拟后端(应该如此),它无法识别返回的 Observable。

如果我将模拟更改为:

let backend = {
  get: () => of({items: []})
}

然后测试成功。

这是测试:


describe('MissionsEffects', () => {
  let actions: Observable<any>;
  let effects: MissionsEffects;
  let backend = {
    get: () => of({items: []})
  }

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        NxModule.forRoot(),
        StoreModule.forRoot({}),
        EffectsModule.forRoot([]),
        RouterModule.forRoot([])
      ],
      providers: [
        MissionsEffects,
        DataPersistence,
        provideMockActions(() => actions),
        { provide: APP_BASE_HREF, useValue: '/' },
        { provide: BackendClientService, useValue: backend }
      ]
    });

    effects = TestBed.get(MissionsEffects);
  });

  describe('loadMissions$', () => {
    it('should work', () => {
      actions = hot('-a-|', { a: new LoadMissions() });
      expect(effects.loadMissions$).toBeObservable(
        hot('-a-|', { a: new MissionsLoaded([]) })
      );
    });
  });
});


我可以确认问题不是由于使用@nrwl/DataPersistence,因为以下@Effect 会产生相同的错误:


@Injectable()
export class MissionsEffects {

  @Effect() loadMissions$ = this.actions$
    .pipe(
      ofType(MissionsActionTypes.LoadMissions),
      switchMap(loadMission => {
        return from(this.backend.get(new GetMissions())
          .then(r => new MissionsLoaded(r.items))
          .catch(e => new MissionsLoadError(e)))
      })
  )

  constructor(
    private actions$: Actions,
    private backend: BackendClientService
  ) {}
}

有人可以帮我准确理解这里的问题吗?为什么我不能使用模拟的 Promise 来测试这个?

4

1 回答 1

0

您可以尝试包装您的测试async()以确保所有异步调用都已完成。虽然resolve是同步的,但from会添加一个.then并且仍然会等待它作为微任务解决。用or包装你的it函数可以解决这个问题。asyncfakeAsync

于 2019-06-11T01:07:33.723 回答