预期的 spyOn 函数将被称为 Jest

编程入门 行业动态 更新时间:2024-10-27 18:20:09
本文介绍了预期的 spyOn 函数将被称为 Jest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前正在尝试测试我创建的方法以及我的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 22:27:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1401094.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将被   函数   spyOn   Jest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!