我正在使用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而不访问它作为一个jest模拟函数?
发布于 2022-05-16 11:58:18
你需要创建一个这样的间谍:
const spy = jest.spyOn(mock, 'onGet');
mock.onGet('https://www.whatever.com').reply(200);
// only if mock client has been called with https://www.whatever.com the test will pass
expect(spy).toHaveBeenCalled();https://stackoverflow.com/questions/56499156
复制相似问题