我只是使用简单的旧玩笑来测试新的Saga动作。经过大量的努力后,我了解到,与其嘲笑响应,不如将响应包含在对生成器的下一次调用中。通过以下功能的以下测试:
import { put, call, takeEvery } from "redux-saga/effects";
export function* loginSaga({ username, password }) {
const creds = { username, password };
try {
let data = yield call(() => axios.get(ApiUrls.login, { params: creds }));
yield put({ type: "LOGIN_SUCCESS", user: data });
} catch (error) {
yield put({ type: "LOGIN_FAILURE", error });
}
}
it("should return a user object on a successful login", () => {
gen = loginSaga(success.creds);
gen.next(); // call api
expect(gen.next(loginResponse.success).value).toEqual(
put({ type: "LOGIN_SUCCESS", user: loginResponse.success.data })
);
});
但是请稍等!测试根本不考虑凭据!无论我使用什么凭证loginSaga()
都可以通过测试,因为它实际上不是在进行API调用,甚至不是模拟调用!
我已经设置了这些对象,以将自己的呼叫反馈给fetchMock
或axios-mock-adapter
:
success = {
url: ApiUrls.Login + "?username=testuser1&password=123123",
creds: { username: "testuser1", password: "123123" }
};
badUser = {
url: success.url.replace("testuser", "xxx"),
creds: { username: "xxx", password: "123123" }
};
badPw = {
url: success.url + "0",
creds: { username: "testuser1", password: "1231230" }
};
但是这种测试方法没有使用它们!
我应该如何真正使该测试有效?