2016-12-07 12 views
0

rxjsオブザーバーのキャッチ機能をテストしようとしていますが、決してキャッチしません。redux-observable epicのスタブキャッチ

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 }); 
    }); 

エピックObservable.ajax.posthttps://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.')); 
     }); 
    }); 
} 

答えて

3

あなたのスタブは、エラーをスローObservableを返す必要がある参照してください。

すべて一緒に
.returns(Observable.throw(new Error())); 

:ちょうど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 

は、あなたは黙ってどこかに何か嚥下エラーを持っているので、外を見るために何かがあります。

関連する問題