单元测试因Angular Ngrx应用程序中的效果而失败

时间:2019-05-02 20:10:11

标签: angular unit-testing jasmine ngrx ngrx-effects

在Angular-ngrx应用程序中,我正在编写两个单元测试以测试服务响应的结果。不管我更改什么,测试似乎总是可以通过。我想念什么吗?

// effects.ts

@Injectable()
export class ContactInfoEffect {
  @Effect()
  fetchContactInfoResponse = this.actions.pipe(
    ofType(CONTACT_INFO_RESPONSE_REQUESTED),
    delay(250),
        mergeMap(contactInfo =>
            this.contactTriageService.getContentInfo().
            pipe(map(contacts => new ContactInfoSucceeded(contacts)),
             ),
             catchError(() => of(new ContactInfoFailed()))
        )
  )

  constructor(
    private actions: Actions,
    private contactTriageService: ContactTriageService
  ) { }
}

// spec.ts

 it('should dispatch `ContactInfoSucceeded` if the service returns successfully', () => {
    const response:ContactInfoResponse =  getContactInfoMock();
    const contactTriageServiceSpy = spyOn(
        contactTriageService,
      'getContentInfo'
    ).and.returnValue(of( [
        getContactInfoMock()
      ]));
    actions = new ReplaySubject(1);
    actions.next(new ContactInfoSucceeded(getContactInfoMock()));
    effects.fetchContactInfoResponse.subscribe((result) => {
      expect(contactTriageServiceSpy).toHaveBeenCalled();
      expect(result).toEqual(new ContactInfoSucceeded(response));
    });
  });

  it('should dispatch `ContactInfoFailed` if the service fails', () => {
    // const response:ContactInfoResponse =  getContactInfoMock();
    // const response =  null;
    const contactTriageServiceSpy = spyOn(
        contactTriageService,
      'getContentInfo'
    ).and.returnValue(throwError(null));
    actions = new ReplaySubject(1);
    actions.next(new ContactInfoFailed());
    effects.fetchContactInfoResponse.subscribe((result) => {
      expect(contactTriageServiceSpy).toHaveBeenCalled();
      expect(result).toEqual(new ContactInfoSucceeded(getContactInfoMock()));
    });
  });

0 个答案:

没有答案