Unit testing 我对axios方法的测试用例说返回类型是匿名函数
要测试的方法是Unit testing 我对axios方法的测试用例说返回类型是匿名函数,unit-testing,jestjs,axios,enzyme,jest-mock-axios,Unit Testing,Jestjs,Axios,Enzyme,Jest Mock Axios,要测试的方法是 const fetchCard = (id) => async (dispatch) => { const response = await axiosWrapper.get(`/giftCards/${id}`); dispatch ({ type: FETCH_CARD, payload: response }) } 测试文件为: import mockAxios from "axios"; import
const fetchCard = (id) => async (dispatch) => {
const response = await axiosWrapper.get(`/giftCards/${id}`);
dispatch ({
type: FETCH_CARD,
payload: response
})
}
测试文件为:
import mockAxios from "axios";
import { fetchCard } from '../actions'
describe('Test GiftCards transaction', () => {
it("fetchCard from Id", async () => {
const mockData = {
"id": 44,
"senderEmail": "abc@gmail.com",
"receiverEmail": "abc@gmail.com",
"cardName": "Food Card",
"cardPoints": "321",
"cardShortDesc": "30% OFF",
"cardImage": "https://images.gyfthd.png",
"cardIssueDate": "Sun May 19 2019 15:43:25 GMT+0530 (India Standard Time)",
"cardExpiryDate": "2019-05-31T00:00:00.000Z",
"isRedeemed": false
}
mockAxios.get.mockImplementationOnce(() =>
Promise.resolve({ data: mockData }),
)
const result = await fetchCard(44);
console.log(result)
expect(mockAxios.get).toHaveBeenCalledTimes(1);
});
})
src/mocks/axios.js
接收值:
预计:{cardExpiryDate:2019-05-31T00:00:00.000Z,cardImage:,cardIssueDate:2019年5月19日星期日15:43:25 GMT+0530印度标准时间,cardName:Food卡,cardPoints:321,cardShortDesc:30折,
id:44,要求:假,收件人电子邮件:abc@gmail.com,senderEmail:abc@gmail.com}
Received:[函数匿名]fetchCard是一个双箭头函数,因此需要调用它两次才能真正调用它
const fetchCard = (id) => async (dispatch) => {
调用fetchCard44返回分派thunk的函数。您需要调用fetchCard44dispatch,或者使用中间件以便调用dispatchfetchCard44
const fetchCard = (id) => async (dispatch) => {