简体   繁体   English

模拟类时,接收到的值必须是模拟或间谍函数

[英]Received value must be a mock or spy function when mocking a class

I am writing a test to check that one method is called when the callback is executed.我正在编写一个测试来检查在执行回调时是否调用了一个方法。 However, it is getting me this error.但是,它让我出现了这个错误。

expect(received).toHaveBeenCalled()

Matcher error: received value must be a mock or spy function

Received has value: undefined

According to Jest automatic class mocks adding the line jest.mock(...) should be enough to get a mocked class, but it seems that I am missing something here.根据Jest 自动类jest.mock(...)添加行jest.mock(...)应该足以获得jest.mock(...)类,但似乎我在这里遗漏了一些东西。

This is my test file:这是我的测试文件:

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();
    });
});

The callback function looks like this, and it is actually being executed :回调函数看起来像这样,它实际上正在执行

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);
};

I've also tried to use the mockImplementation method, but with no luck at all.我也尝试过使用mockImplementation方法,但完全没有运气。 Any suggestion is well-received.任何建议都很受欢迎。

Jest version: 26.6.0 React version: 17.0.2 React-testing-library version: ^12.1.2 Jest 版本:26.6.0 React 版本:17.0.2 React-testing-library 版本:^12.1.2

The run method is called by an instance of the DetectFibonacciUseCase class, not by the class itself. run方法由DetectFibonacciUseCase类的实例调用,而不是由类本身调用。

While the Jest automock will work as expected, you need to access the mock class instance to check if the run function has been called.虽然 Jest 自动模拟将按预期工作,但您需要访问模拟类实例以检查run函数是否已被调用。

const mockDetectFibonacciUseCaseInstance = DetectFibonacciUseCase.mock.instances[0];
expect(mockDetectFibonacciUseCaseInstance.run).toHaveBeenCalled();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jest 函数必须是模拟或间谍 - jest function must be a mock or spy 为什么在React单元测试中的模拟事件中使用sinon.spy模拟函数和React Component的asen.shallow渲染时,这是未定义的? - Why is this is undefined when mocking event in React unit testing, using sinon.spy to mock function and enzyme.shallow render for React Component? 在模拟文件中窥探 function - Spy function in mock file 如何监视一个函数并使用茉莉花从另一个函数内部返回模拟值? - How can I spy on a function and return a mock value from inside another function using jasmine? 我可以模拟/监视非纯函数的父作用域中存在的变量值吗? - Can I mock / spy on a variable value that exists in the parent scope of a non-pure function? 开玩笑测试应该断言一个异常,但接收到的值必须是 function - Jest test should assert one exception but received value must be a function Jest 的匹配器错误:接收到的值必须是 function - Jest's matcher error: received value must be a function 开玩笑的单元测试。 模拟函数返回,监视函数调用 - Jest unit test. Mock function return, spy on function call 在测试子类时,如何监视祖父班中的导入函数? - How can I spy on an imported function in a grandparent class when testing a child class? 如何监视从 Jest 中的模拟返回的 object function? - How to spy on an object function returned from a mock in Jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM