【问题标题】:expected spyOn function to be called Jest预计 spyOn 函数将被称为 Jest
【发布时间】:2020-04-08 23:05:50
【问题描述】:

我目前正在尝试测试我创建的方法,并且我的 if 语句中的方法没有被调用。我完全不知所措。我是新人太开玩笑了,所以我确定我缺少一些简单的东西。

 describe('isSingleScreen', () => {
    beforeEach(() => {
      jest.clearAllMocks();
      jest.spyOn(utilMethods, 'isDualScreen').mockReturnValue(true);
    });

    it('autoScreenAdd', () => {
      // Arrange
      const singleScreenAddSpy = jest.spyOn(
        singleScreenMethods,
        'singleScreenAdd'
      );
      const dualScreenAddSpy = jest.spyOn(dualScreenMethods, 'dualScreenAdd');

      // Act
      utilMethods.autoScreenAdd({});

      // Assert
      expect(singleScreenAddSpy).toBeCalledTimes(0);
      expect(dualScreenAddSpy).toBeCalled();
      expect(dualScreenAddSpy).toBeCalledTimes(1);
    });
  });

export const isDualScreen = (): boolean => {
  return Dimensions.get('window').width > 1000 ? true : false;
};

export const autoScreenAdd = (element: IDualComponent) => {
  if (isDualScreen()) {
    dualScreenAdd(element);
  } else {
    singleScreenAdd(element);
  }
};

这是我收到的错误

    expect(jest.fn()).toBeCalledTimes(expected)

    Expected number of calls: 0
    Received number of calls: 1

      30 |       // Assert
      31 |       expect(autoScreenAddSpy).toBeCalled();
    > 32 |       expect(singleScreenAddSpy).toBeCalledTimes(0);
         |                                  ^
      33 |       expect(dualScreenAddSpy).toBeCalled();
      34 |       expect(dualScreenAddSpy).toBeCalledTimes(1);
      35 |     });

【问题讨论】:

    标签: reactjs typescript react-native testing jestjs


    【解决方案1】:

    如何测试包含在同一模块中调用其他函数的函数的模块存在限制。 See this article for some more insight。在那篇文章中解决这个问题的方法很少,所以我建议在深入研究我的粗略实现之前先看看它,因为它可能无法 100% 使用你的代码结构。

    工作示例:

    CodesandBox

    实用程序

    对您的原始版本进行了轻微修改,因此您可能需要根据需要在您的 util 模块中进行模仿。

    const isDualScreen = () => {
      return window.width > 1000 ? true : false;
    };
    
    const autoScreenAdd = element => {
      if (utilMethods.isDualScreen()) {
        utilMethods.dualScreenAdd(element);
      } else {
        utilMethods.singleScreenAdd(element);
      }
    };
    
    const dualScreenAdd = element => {
      return element;
    };
    
    const singleScreenAdd = element => {
      return element;
    };
    
    // This is important, it allows you to mock the functions properly in your tests.
    // Use this same structure in your singleScreenMethods and dualScreenMethods modules
    const utilMethods = {
      singleScreenAdd,
      dualScreenAdd,
      autoScreenAdd,
      isDualScreen
    };
    
    export default utilMethods;
    
    

    测试示例

    import utilMethods from "./utils";
    
    describe("isSingleScreen", () => {
      beforeEach(() => {
        jest.clearAllMocks();
        jest.spyOn(utilMethods, "isDualScreen").mockReturnValue(true);
      });
    
      it("autoScreenAdd", () => {
        // Arrange
        const singleScreenAddSpy = jest.spyOn(utilMethods, "singleScreenAdd");
        const dualScreenAddSpy = jest.spyOn(utilMethods, "dualScreenAdd");
    
        // Act
        utilMethods.autoScreenAdd({});
    
        // Assert
        expect(singleScreenAddSpy).toHaveBeenCalledTimes(0);
        expect(dualScreenAddSpy).toHaveBeenCalledTimes(1);
      });
    });
    

    【讨论】:

    • 我有,但不幸的是它返回了相同的结果。尽管作为最佳实践,请记住它:)
    • @keil 用这些发现和代码沙箱上的工作示例更新了我的答案,希望对您有所帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 2016-03-31
    • 2021-03-14
    • 2021-08-21
    • 2021-11-03
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多