简体   繁体   English

开玩笑:嘲笑条件函数调用

[英]Jest: mocking conditional function calls

I am trying to write a test to make sure that, when appropriate, a particular function (in this case, a sentry function) is called with a particular message. 我正在尝试编写一个测试,以确保在适当的情况下,使用特定的消息调用特定的函数(在这种情况下为哨兵函数)。 However, when I write this test, it fails, and I get the following message. 但是,当我编写此测试时,它失败了,并且收到以下消息。 How to I properly mock the captureMessage function in handleError.test.js to make sure that it is properly called with the "this is an error message." 如何正确模拟handleError.test.jscaptureMessage函数,以确保使用"this is an error message."正确调用了该"this is an error message." string in handleError.js ? handleError.js字符串? Thanks! 谢谢!

error message: 错误信息:

Error: expect(jest.fn())[.not].toHaveBeenCalledWith() 错误:expect(jest.fn())[。not] .toHaveBeenCalledWith()

jest.fn() value must be a mock function or spy. jest.fn()值必须是模拟函数或间谍。 Received: function: [Function captureMessage] 收到:函数:[Function captureMessage]

handleError.js: handleError.js:

import {captureMessage} from '@sentry/browser';

const handleError = (error) => {
  if (error.name === "ApiError") {
    captureMessage('this is an error message.');
  }
};

export default handleError;

handleError.test.js: handleError.test.js:

import {captureMessage} from '@sentry/browser';
import handleError from '../handleError';

class ApiError extends Error {
  constructor() {
    super();
    this.name = 'ApiError';
  }
}

test('When an ApiError is returned with no action type, sentry is notified', () => {
  const sampleError = new ApiError();
  handleError(sampleError);
  expect(captureMessage).toHaveBeenCalledWith('this is an error message.');
});

As @balzee mentioned, you have to actually spy on the method you want to make assertions about. 如@balzee所述,您实际上必须监视要进行断言的方法。 That causes Jest to replace the method with a special spy function that keeps track of the parameters it is called with, how many times it was called, and so on. 这导致Jest用特殊的间谍函数替换该方法,该函数跟踪被调用的参数,被调用的次数等等。

You should also provide a mock implementation for that function, so that you don't actually call out to Sentry when running your unit tests. 您还应该为该函数提供一个模拟实现,这样,在运行单元测试时实际上就不会调出Sentry了。

Finally, when spying on a method, you first pass the object the method is on, and then the name of the method as a string. 最后,在监视方法时,首先传递该方法所在的对象,然后将该方法的名称作为字符串传递。 Jest then replaces that property on the object with a spy function, that will call the original function if no mock implementation is given. 然后,Jest用一个间谍函数替换该对象上的该属性,如果没有给出模拟实现,它将调用原始函数。

Without referencing the object the function exists on, you would just be changing what a local function variable points to, from the original/real function to a jest spy function. 如果不引用函数所在的对象,则只需将本地函数变量指向的对象从原始/实函数更改为开玩笑的间谍函数即可。 That won't change the function that the code you are testing invokes, so the test will fail. 那不会改变您正在测试的代码调用的功能,因此测试将失败。

So the final test should be: 因此,最终测试应为:

handleError.test.js: handleError.test.js:

import * as sentry from '@sentry/browser'; // CHANGED
import handleError from '../handleError';

class ApiError extends Error {
  constructor() {
    super();
    this.name = 'ApiError';
  }
}

// added this to remove any spies/mocks after the test
afterEach(() => {
  jest.restoreAllMocks();
});

test('When an ApiError is returned with no action type, sentry is notified', () => {
  const sampleError = new ApiError();
  // added next line
  jest.spyOn(sentry, 'captureMessage').mockImplementation(() => {});
  handleError(sampleError);
  // note the use of `sentry.captureMessage`, which is now a jest spy fn
  expect(sentry.captureMessage).toHaveBeenCalledWith('this is an error message.');
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM