HTTP请求超时时的单元测试ngrx效果

时间:2019-03-04 12:25:55

标签: angular jasmine angular6 ngrx ngrx-effects

我有一个get请求,我将超时设置为2秒,如果未收到我的响应,则会引发错误。

在服务中

deleteAccount(accountId: string) {
    return this.http.delete(FUNDING_ACCOUNT_DELETE_URL).pipe(
        timeout(RESPONSE_TIME_OUT) // 2 seconds
      );
  }

效果:

paymentRemove = this.actions
    .ofType(ActionTypes.PAYMENT_REMOVE).pipe(
      switchMap((action: PaymentRemoveAction) =>
        this.service.deleteAccount(action.payload).pipe(
          map(
            _ => new PaymentRemovedAction()),
          catchError(e => {
            if (e) {
              return of(new PaymentRemoveErrorAction());
            }

          })
        ))
    );

在我的效果单元测试文件中,我有

class TestMock {

  deleteAccount(accountId: string) {
    return Observable.create(async observer => {
      if (accountId === ACCOUNT_ID) {
        observer.next('');
        return;
      }
      observer.error('Error');
    });
  }
}

在我的描述块中,我也有

beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        TestEffects,
        {provide: AddService, useClass: TestMock},
        {provide: Router, useClass: RouterMock},
        provideMockActions(() => actions)
      ],
      imports: [
        StoreModule.forRoot({
          payment: addtReducer
        })
      ]
    });
    effects = TestBed.get(TestEffects);
  });

这是我积极的测试用例  // tslint:disable-next-line:ban

fit('delete payment option - success', marbles(m => {
    const action = new PaymentRemoveAction(ACCOUNT_ID);
    const completion = new PaymentRemovedAction();
    const expected = m.hot('b', {b: completion});
    actions = m.hot('a', {a: action});
    m.expect(effects.paymentRemove).toBeObservable(expected);
  }));

如何为效果而不是service.ts文件中的超时编写否定测试用例

0 个答案:

没有答案