【发布时间】:2018-07-08 11:01:08
【问题描述】:
我是单元测试、Redux 和 Redux Saga 的新手。我将它与 React Native 一起使用。我刚刚实现了我的loginFlow,就像docs of Redux Saga 中描述的那样:
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
// fork return a Task object
const task = yield fork(authorize, user, password)
const action = yield take(['LOGOUT', 'LOGIN_ERROR'])
if (action.type === 'LOGOUT')
yield cancel(task)
yield call(Api.clearItem, 'token')
}
}
function* authorize(user, password) {
try {
const token = yield call(Api.authorize, user, password)
yield put({type: 'LOGIN_SUCCESS', token})
yield call(Api.storeItem, {token})
return token
} catch(error) {
yield put({type: 'LOGIN_ERROR', error})
} finally {
if (yield cancelled()) {
// ... put special cancellation handling code here
}
}
}
我现在正在尝试为此身份验证流程编写测试。不幸的是,我无法弄清楚如何为*loginFlow 生成器提供电子邮件和密码的测试。这是我目前所在的位置:
describe("login flow", () => {
const gen = loginFlow();
const user = "test@test.test";
const password = "testpassword";
it("should wait for a user to log in", () => {
expect(gen.next({ user, password }).value).toEqual(take(LOGIN_REQUEST));
});
it("should fork the handling of the login request", () => {
const expectedYield = fork(authorize, user, password);
expect(gen.next().value).toEqual(expectedYield);
});
});
问题是,这会引发错误:
● login flow › should fork the handling of the login request
TypeError: Cannot read property 'email' of undefined
28 | export function* loginFlow() {
29 | while (true) {
> 30 | const { email, password } = yield take(LOGIN_REQUEST);
| ^
31 | // fork return a task object
32 | const task = yield fork(authorize, email, password);
33 | const action = yield take([LOGOUT, LOGIN_ERROR]);
如您所见,我尝试通过在下一次调用中提供这些值来将这些值提供给 yield 关键字,但它不起作用。
【问题讨论】:
标签: unit-testing redux jestjs redux-saga redux-saga-test-plan