2

我正在使用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在不访问它的情况下将其用作开玩笑的模拟函数?

4

0 回答 0