存根在Redux-Observable Epic上



i正在尝试测试RXJS观察者的捕获功能,但永远不会运行。

test.js

it.only('On fail restore password', () => {
    sandbox = sinon.stub(Observable.ajax, 'post').returns(new Error());
    store.dispatch(restorePasswordVi('prueba'));
    expect(store.getActions()).toInclude({ success: false, type: SET_RESTORE_PASSWORD_SUCCESS });
  });

史诗请参阅https://redux-observable.js.org/

export function restorePasswordViEpic(action$: Observable<Action>, store: Store) {
  return action$
    .ofType(RESTORE_PASSWORD_VI)
    .switchMap(({ user }) => {
      store.dispatch(blockLoading(true));
      return Observable
       .ajax
       .post(`${config.host}/auth/restore-password`, { user })
         .map(() => setRestorePasswordSuccess(true))
         .catch(() => {
           return Observable.of(setMessage('Se ha producido un error por favor intente de nuevo.'));
         });
    });
}

您对Observable.ajax.post的固执需要返回引发错误的Observable

.returns(Observable.throw(new Error()));

一起:

it.only('On fail restore password', () => {
  sandbox = sinon.stub(Observable.ajax, 'post').returns(Observable.throw(new Error()));
  store.dispatch(restorePasswordVi('prueba'));
  expect(store.getActions()).toInclude({ success: false, type: SET_RESTORE_PASSWORD_SUCCESS });
});

由于您现有的存根仅返回一个错误对象本身(不是可观察到的错误),它应该导致丢弃的未被发现的错误,例如:

Uncaught TypeError: Observable.ajax.post(...).map is not a function

如果您在进行测试时看不到任何错误,则可能会在某个地方静静地吞咽错误,因此要注意的东西。

最新更新