模拟 class 时接收到的值必须是模拟或间谍函数
Received value must be a mock or spy function when mocking a class
我正在编写一个测试来检查执行回调时是否调用了一个方法。但是,它给我带来了这个错误。
expect(received).toHaveBeenCalled()
Matcher error: received value must be a mock or spy function
Received has value: undefined
根据 Jest automatic class mocks 添加行 jest.mock(...)
应该足以得到一个模拟 class,但似乎我在这里遗漏了一些东西。
这是我的测试文件:
import { render, fireEvent } from "@testing-library/react";
import Grid from "../../components/Grid";
import DetectFibonacciUseCase from "../../useCases/DetectFibonacciUseCase";
jest.mock("../../useCases/DetectFibonacciUseCase");
describe("GridComponent", () => {
test("Should've called the run method when the callback is executed", () => {
const { getByTestId } = render(<Grid />);
const firstCellButton = getByTestId("cell-testid-0-0");
fireEvent.click(firstCellButton);
expect(DetectFibonacciUseCase.run).toHaveBeenCalled();
});
});
回调函数看起来是这样的,它实际上正在执行:
const calculateNewValues = (row, column) => {
const updatedCells = cells.map((cell) => {
cell.value = cell.row === row || cell.column === column
? cell.value + 1
: cell.value;
cell.color = cell.row === row || cell.column === column ? ColorConstants.yellow : cell.color;
return cell;
});
const detectFibonacciUseCase = new DetectFibonacciUseCase(
MINIMUM_CONSECUTIVE_APPAREANCES
);
const cellsWithFibonacci = detectFibonacciUseCase.run(updatedCells);
cellsWithFibonacci.forEach((cellWithFibonacci) => {
const cellToUpdateIndex = updatedCells.findIndex(
(cell) =>
cell.row === cellWithFibonacci.row &&
cell.column === cellWithFibonacci.column
);
updatedCells[cellToUpdateIndex].color = ColorConstants.green;
updatedCells[cellToUpdateIndex].value = 1;
});
setCells(updatedCells);
removeColorsAfterTimeout(updatedCells);
};
我也尝试过使用 mockImplementation
方法,但一点都不成功。
任何建议都会受到欢迎。
开玩笑版本:26.6.0
反应版本:17.0.2
反应测试库版本:^12.1.2
run
方法由 DetectFibonacciUseCase
class 的实例调用,而不是由 class 本身调用。
虽然 Jest automock 将按预期工作,但您需要访问模拟 class 实例以检查是否已调用 run
函数。
const mockDetectFibonacciUseCaseInstance = DetectFibonacciUseCase.mock.instances[0];
expect(mockDetectFibonacciUseCaseInstance.run).toHaveBeenCalled();
我正在编写一个测试来检查执行回调时是否调用了一个方法。但是,它给我带来了这个错误。
expect(received).toHaveBeenCalled()
Matcher error: received value must be a mock or spy function
Received has value: undefined
根据 Jest automatic class mocks 添加行 jest.mock(...)
应该足以得到一个模拟 class,但似乎我在这里遗漏了一些东西。
这是我的测试文件:
import { render, fireEvent } from "@testing-library/react";
import Grid from "../../components/Grid";
import DetectFibonacciUseCase from "../../useCases/DetectFibonacciUseCase";
jest.mock("../../useCases/DetectFibonacciUseCase");
describe("GridComponent", () => {
test("Should've called the run method when the callback is executed", () => {
const { getByTestId } = render(<Grid />);
const firstCellButton = getByTestId("cell-testid-0-0");
fireEvent.click(firstCellButton);
expect(DetectFibonacciUseCase.run).toHaveBeenCalled();
});
});
回调函数看起来是这样的,它实际上正在执行:
const calculateNewValues = (row, column) => {
const updatedCells = cells.map((cell) => {
cell.value = cell.row === row || cell.column === column
? cell.value + 1
: cell.value;
cell.color = cell.row === row || cell.column === column ? ColorConstants.yellow : cell.color;
return cell;
});
const detectFibonacciUseCase = new DetectFibonacciUseCase(
MINIMUM_CONSECUTIVE_APPAREANCES
);
const cellsWithFibonacci = detectFibonacciUseCase.run(updatedCells);
cellsWithFibonacci.forEach((cellWithFibonacci) => {
const cellToUpdateIndex = updatedCells.findIndex(
(cell) =>
cell.row === cellWithFibonacci.row &&
cell.column === cellWithFibonacci.column
);
updatedCells[cellToUpdateIndex].color = ColorConstants.green;
updatedCells[cellToUpdateIndex].value = 1;
});
setCells(updatedCells);
removeColorsAfterTimeout(updatedCells);
};
我也尝试过使用 mockImplementation
方法,但一点都不成功。
任何建议都会受到欢迎。
开玩笑版本:26.6.0 反应版本:17.0.2 反应测试库版本:^12.1.2
run
方法由 DetectFibonacciUseCase
class 的实例调用,而不是由 class 本身调用。
虽然 Jest automock 将按预期工作,但您需要访问模拟 class 实例以检查是否已调用 run
函数。
const mockDetectFibonacciUseCaseInstance = DetectFibonacciUseCase.mock.instances[0];
expect(mockDetectFibonacciUseCaseInstance.run).toHaveBeenCalled();