我有一个POST
到我的API的createPostSaga,我正在尝试对其进行测试。这是传奇:
export function* createPostSaga(action) {
const token = yield select(selectToken);
const headerParams = {
Authorization: `JWT ${token}`
};
const apiCall = () => {
let formData = new FormData();
formData.append("title", action.payload.title);
formData.append("content", action.payload.content);
formData.append("thumbnail", action.payload.thumbnail);
formData.append("tags", JSON.stringify(action.payload.tags));
formData.append("slug", generateSlug(action.payload.title));
return axios({
method: "post",
url: "/posts/",
data: formData,
headers: headerParams
})
.then(response => response.data)
.catch(err => {
throw err;
});
};
try {
const response = yield call(apiCall);
yield put(push(`/posts/${response.id}/${response.slug}/`));
} catch (error) {
console.log(error);
}
}
这是我到目前为止失败的代码:
describe("createPostSaga", () => {
const action = {
type: types.CREATE_POST,
payload: {
title: "test title",
pub_date: "2018-11-12",
content: "test content",
tags: ["test1", "test2"],
thumbnail: "http://fail",
slug: "test-title"
}
}
const apiCall = () => ({
id: 1,
...action.payload
});
it("calls the api and redirects", () => {
testSaga(actions.createPostSaga, action)
.next()
.select(selectToken)
.next()
.call(apiCall)
.next()
.put(push("/posts/1/test-title/"))
.finish()
.isDone();
})
})
测试此代码时,出现以下错误:
SagaTestError:
Assertion 2 failed: call effects do not match
Expected
--------
{ context: null, fn: [Function: apiCall], args: [] }
Actual
------
{ context: null, fn: [Function: apiCall], args: [] }
如您所见,预期结果与实际结果相同。
我还试图将apiCall
函数直接复制粘贴到我的测试中,但是它失败,并显示相同的消息,我很困惑。