我可以在axios-mock-adapter中使用与jest.fn()相关的检查吗?

时间:2019-06-07 17:54:02

标签: jestjs axios-mock-adapter

我正在使用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作为玩笑的模拟函数而不能访问它?

0 个答案:

没有答案