预期的 spyOn 函数将被称为 Jest [英] expected spyOn function to be called Jest

查看:35
本文介绍了预期的 spyOn 函数将被称为 Jest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试测试我创建的方法以及我的 if 语句中的方法没有被调用.我完全不知所措.我也是新手,所以我确定它是我遗漏的一些简单的东西.

im currently trying to test the methods that i have created and the methods in my if statement are not being called. and im completely at a loss. I am new too jest though so im sure its something simple im missing.

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

推荐答案

测试包含调用同一模块内其他函数的函数的模块的方式存在限制.请参阅这篇文章更多洞察.在那篇文章中几乎没有方法可以解决这个问题,因此我建议在深入研究我的粗略实现之前先看看它,因为它可能无法 100% 使用您的代码结构.

There is a limitation to how you can test a module that contains functions which call other functions within the same module. See this article for some more insight. There are few ways to work around this in that article, so I recommend taking a look at that first before diving into my rough implementation as it may not work with your code structure 100%.

CodesandBox

对您的原始文件略有修改,因此您可能需要根据需要在您的 util 模块中模仿这一点.

Slightly modified from your original, so you may need to mimic this in your util modules as necessary.

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

这篇关于预期的 spyOn 函数将被称为 Jest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆