我正在使用axios-mock-adapter
,我想知道是否可以对其进行设置,以便我也可以像expect(MockAdapter.get).toHaveBeenCalledWith(url);
这样的断言
我尝试了以下操作:
// test
import MockAdapter from "axios-mock-adapter";
import { fetchProducts } from "../src/redux/actions/productActions";
describe("fetchProducts", () => {
const mock = new MockAdapter();
it("fetches the products", async () => {
const x = await fetchProducts();
expect(mock.get).toHaveBeenCalledWith("https://www.whatever.com");
});
});
// function
import axios from "axios";
export async function* fetchProducts() {
let data = await axios.get("https://www.whatever.com");
return data;
}
它说
expect(jest.fn())[.not].toHaveBeenCalledWith()
Matcher error: received value must be a mock or spy function
Received has value: undefined
如果我添加一个./__mocks__/axios.js
文件,我会立即得到
TypeError: axios.create is not a function
> 1 | import MockAdapter from "axios-mock-adapter";
| ^
2 | import { fetchProducts } from "../src/redux/actions/productActions";
有没有办法做到这一点,或者它是模仿我自己的axios
函数的实现,还是使用axios-mock-adapter
作为玩笑的模拟函数而不能访问它?