javascript Jest onSpy - 预期的模拟函数已被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50719494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:14:32  来源:igfitidea点击:

Jest onSpy - expected mock function to have been called

javascriptunit-testingmockingjestjsspy

提问by terjeofnorway

I'm struggling with using spyOn as part of testing my utils.js module. I've tried various methods and approaches but all seem to yield the "expected mock function to have been called". For the record, other unit tests work OK, so there shouldn't be any issue with my actual test setup.

我正在努力使用 spyOn 作为测试我的 utils.js 模块的一部分。我尝试了各种方法和方法,但似乎都产生了“预期的模拟函数已被调用”。作为记录,其他单元测试工作正常,所以我的实际测试设置应该没有任何问题。

Below is a simplified test case with two functions and one test, and I can't even get these to work. Did I misunderstand the spyOn altogether?

下面是一个包含两个函数和一个测试的简化测试用例,我什至无法让它们工作。我完全误解了间谍吗?

// utils.js
function capitalHelper(string){
  return string.toUpperCase();
}

function getCapitalName(inputString){
  return capitalHelper(inputString.charAt(0)) + inputString.slice(1);
}

exports.capitalHelper = capitalHelper
exports.getCapitalName = getCapitalName



// utils.test.js
const Utils = require('./utils');

test('helper function was called', () => {
  const capitalHelperSpy = jest.spyOn(Utils, 'capitalHelper');
  const newString = Utils.getCapitalName('john');
  expect(Utils.capitalHelper).toHaveBeenCalled();
})

采纳答案by Ji aSH

I do ont use spyOn(), but jest.fn() instead for all mock scenario

我不使用 spyOn(),而是使用 jest.fn() 代替所有模拟场景

In your case I would do the following

在你的情况下,我会做以下

test('helper function was called', () => {
    Utils.capitalHelper = jest.fn((s) => Utils.capitalHelper(s))
    const newString = Utils.getCapitalName('john')
    expect(Utils.capitalHelper.mock.calls.length).toBe(1)
})

First line could have simply be :

第一行可能只是:

Utils.capitalHelper = jest.fn()

since you don't seem to be testing the returned value in your test :)

因为您似乎没有在测试中测试返回值:)

You can find more details on jest.fn() on the jest official documentation : https://facebook.github.io/jest/docs/en/mock-functions.html

您可以在 jest 官方文档中找到有关 jest.fn() 的更多详细信息:https://facebook.github.io/jest/docs/en/mock-functions.html

----------------------- EDIT

- - - - - - - - - - - - 编辑

I got it : the problem occurs because within your utils.js file, getCapitalName uses the defined function, not the one pointed by the export.

我明白了:出现问题是因为在您的 utils.js 文件中, getCapitalName 使用定义的函数,而不是导出指向的函数。

To be able to mock the function in use you could change your utils.js file to

为了能够模拟正在使用的函数,您可以将 utils.js 文件更改为

// utils.js
const Utils = {
    capitalHelper: string => string.toUpperCase(),
    getCapitalName: inputString => Utils.capitalHelper(inputString.charAt(0)) + inputString.slice(1)
}

export default Utils

then the tests I gave before will work

那么我之前给出的测试将起作用