我试图测试我的传奇可以遵循的每个场景,但我无法实现我想要的行为.这很简单,我有一个HTTP请求(登录),我想通过模拟我的API方法来测试成功和失败的情况.
但是,它看起来call effect不会激活我的api功能,我还没有真正得到它是如何工作的,但我想中间件是负责调用该功能,因为我不去通过商店我的测试,我无法得到结果.
所以我的问题是,当您需要在异步调用旁边发送不同的操作(通常是成功或失败)时,如何测试您的传奇?
我找了一个例子,我发现sagas成功并失败但是失败案例从未经过测试,例如在这里的购物车示例中
SAGA.JS
export function* login(action) {
try {
const user = yield call(api.login, action);
return yield put(actions.loginSuccess(user));
} catch(e) {
yield put(actions.loginFail(e));
}
}
export default function* rootAuthenticationSagas() {
yield* takeLatest(LOGIN, login);
}
Run Code Online (Sandbox Code Playgroud)
TEST.JS
describe('login', () => {
context('When it fails', () => {
before('Stub the api', () => {
sinon.stub(api, 'login', () => {
// IT NEVER COMES HERE !
return Promise.reject({ error: 'user not found' });
});
});
it('should return …Run Code Online (Sandbox Code Playgroud)