Sagas 测试中未调用我的模拟函数
My mock function isn't being called in Sagas test
我正在尝试用 Jest 测试 Sagas,但我无法让 Sagas 调用 loginApi 模拟函数,似乎它正在调用实际函数。有人可以帮我解决这个问题吗?
这是我的传奇:
export async function loginApi(user) {
return await api.post('/auth/customers/login', {
email: user.email, //customer1@mi.me
password: user.password, //12345678
});
}
// Saga function that handles the side effect when the loginWatcher is triggered
export function* loginActionEffect(loginAction) {
//console.tron.log('login Action Effect');
try {
const response = yield call(loginApi, loginAction.user);
// console.log('response:' + response);
yield AsyncStorage.setItem('access_token', response.data.access_token);
yield put(LoginActions.setLogin(loginAction.user));
yield put(LoginActions.setError(''));
} catch (e) {
//console.log('problema com o login!');
yield put(LoginActions.setError('Email ou senha incorretos!'));
}
}
这是我的测试:
it('should receive token in case of success', async () => {
const mockToken = {token: 'kaajfalkfjlaniurvayeg'};
const loginApi = jest.fn().mockImplementation(() => Promise.resolve(mockToken));
const dispatched = [];
const result = await runSaga(
{
dispatch: action => dispatched.push(action),
},
loginActionEffect,
);
expect(loginApi).toHaveBeenCalled();
expect(dispatched).toContainEqual(Creators.setLogin());
expect(dispatched).toContainEqual(Creators.setError());
loginApi.mockClear();
});
这是我的测试结果:
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
21 | );
22 |
> 23 | expect(loginApi).toHaveBeenCalled();
| ^
runSaga
returns 一个 Task
对象不是 Promise
所以你需要调用 toPromise
const result = await runSaga(
{
dispatch: action => dispatched.push(action),
},
loginActionEffect,
).toPromise();
PS。您测试 sagas 的方式与推荐的不符。请阅读此 docs。正如我在评论中所写。关于声明式效果的一件很酷的事情是效果执行依赖于解释器。因此,您无需在测试中实际执行或模拟任何内容。
我正在尝试用 Jest 测试 Sagas,但我无法让 Sagas 调用 loginApi 模拟函数,似乎它正在调用实际函数。有人可以帮我解决这个问题吗?
这是我的传奇:
export async function loginApi(user) {
return await api.post('/auth/customers/login', {
email: user.email, //customer1@mi.me
password: user.password, //12345678
});
}
// Saga function that handles the side effect when the loginWatcher is triggered
export function* loginActionEffect(loginAction) {
//console.tron.log('login Action Effect');
try {
const response = yield call(loginApi, loginAction.user);
// console.log('response:' + response);
yield AsyncStorage.setItem('access_token', response.data.access_token);
yield put(LoginActions.setLogin(loginAction.user));
yield put(LoginActions.setError(''));
} catch (e) {
//console.log('problema com o login!');
yield put(LoginActions.setError('Email ou senha incorretos!'));
}
}
这是我的测试:
it('should receive token in case of success', async () => {
const mockToken = {token: 'kaajfalkfjlaniurvayeg'};
const loginApi = jest.fn().mockImplementation(() => Promise.resolve(mockToken));
const dispatched = [];
const result = await runSaga(
{
dispatch: action => dispatched.push(action),
},
loginActionEffect,
);
expect(loginApi).toHaveBeenCalled();
expect(dispatched).toContainEqual(Creators.setLogin());
expect(dispatched).toContainEqual(Creators.setError());
loginApi.mockClear();
});
这是我的测试结果:
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
21 | );
22 |
> 23 | expect(loginApi).toHaveBeenCalled();
| ^
runSaga
returns 一个 Task
对象不是 Promise
所以你需要调用 toPromise
const result = await runSaga(
{
dispatch: action => dispatched.push(action),
},
loginActionEffect,
).toPromise();
PS。您测试 sagas 的方式与推荐的不符。请阅读此 docs。正如我在评论中所写。关于声明式效果的一件很酷的事情是效果执行依赖于解释器。因此,您无需在测试中实际执行或模拟任何内容。