[英]Unit testing of a function with conditional statement
I have a function to track analytics from google analytics. 我具有从Google Analytics(分析)跟踪分析的功能。 So that in my function i need to see if the code exists or not.
因此,在我的函数中,我需要查看代码是否存在。
Analytics.js
const gaCode = process.env.REACT_APP_GA_TRACKING_CODE;
const gaCodeExists = gaCode && gaCode.Length > 0;
function trackEvent(category = "Event", name) {
if(gaCodeExists) {
GoogleAnalytics.event({
category,
action: name,
});
}
return;
}
export default {
gaCodeExists,
trackEvent
}
At first I was doing like this (which I am pretty sure I am not doing correctly). 起初,我是这样做的(我敢肯定我做得不好)。
describe('Testing analytics', () => {
beforeEach(() => {
Analytics.trackEvent(null, 'Tracking');
});
it('should not call google analytics', () => {
if (!gaCodeExists) {
const mockGoogleAnalytics = jest.fn(() => GoogleAnalytics.event);
expect(mockGoogleAnalytics.mock.calls.length).toBe(0);
}
});
})
After reading some blog posts and looking at stackover flow questions . 阅读一些博客文章并查看stackover流问题之后 。 I changed it to like this which I think I am mocking the gaCodeExists variable as below.
我将其更改为这样,我认为我在模仿gaCodeExists变量,如下所示。
import constants from '../index';
import { isUsingGoogleAnalytics } from '../index';
describe('Analytics Testing', () => {
it('Should test trackEvent', () => {
constants.isUsingGoogleAnalytics = true;
expect(constants.isUsingGoogleAnalytics).toBe(true);
});
});
Now I am stuck how can I do the testing on trackEvent
function. 现在我被困在如何对
trackEvent
函数进行测试。 How can I apply mock gaCodeExists variable to it? 如何将模拟gaCodeExists变量应用于该变量? The test cases would be if the
gaCodeExists
is true, expect(mockFunction).toHaveBeenCalled(1)
. 如果
gaCodeExists
为true,则gaCodeExists
为expect(mockFunction).toHaveBeenCalled(1)
。
PS: I am new to testing. PS:我是测试新手。
Your gaCodeExists
assigns value to the variable while the js file is loaded. 加载js文件时,您的
gaCodeExists
会为变量分配值。 And trackEvent
function is not pure as it depends on a value that is outside its scope. trackEvent
函数不是纯函数,因为它依赖于其范围之外的值。 It's advisable to make your functions pure, so you can test it the right way. 建议您使函数纯净,以便以正确的方式对其进行测试。 Since this is a GA implementation, you can just convert
gaCodeExists
to a function and mock it in your tests. 由于这是GA实施,因此您可以将
gaCodeExists
转换为函数并在测试中对其进行模拟。
Change your gaCodeExists
to a method 将您的
gaCodeExists
更改为方法
const gaCodeExists = () => {
if(process && process.env && process.env.REACT_APP_GA_TRACKING_CODE) {
const gaCode = process.env.REACT_APP_GA_TRACKING_CODE;
return gaCode && gaCode.Length > 0;
}
return false;
}
Change your trackEvent
function to 将您的
trackEvent
函数更改为
function trackEvent(category = "Event", name) => {
if(gaCodeExists()) { // note this line
GoogleAnalytics.event({
category,
action: name,
});
}
return;
}
Now you can test it by mocking gaCodeExists
function 现在您可以通过
gaCodeExists
函数进行测试
import * as analytics from "../index";
describe('Testing analytics', () => {
beforeEach(() => {
const gaMock = jest.spyOn(analytics, "gaCodeExists");
});
it('should not call google analytics', () => {
gaMock.mockImplementation(() => false);
// check what happens when it doesn't call GA
});
it('should call google analytics', () => {
gaMock.mockImplementation(() => true);
// check what happens when it calls GA
});
})
I haven't tested this code. 我尚未测试此代码。 So please double check if it's doing what it's intended to do.
因此,请仔细检查它是否正在执行预期的工作。
const gaCode = process.env.REACT_APP_GA_TRACKING_CODE;
const gaCodeExists = gaCode && gaCode.Length > 0;
function trackEvent(category = "Event", name, gaCodeExists) {
if(gaCodeExists) {
GoogleAnalytics.event({
category,
action: name,
})
}
}
export default {
gaCodeExists,
trackEvent
}
First change your Analytics code like this, your test file should be like following 首先像这样更改您的Google Analytics(分析)代码,您的测试文件应如下所示
import { UsingGoogleAnalytics } from '../index' // i don't know what this file is
import { gaCodeExistsis, trackEvent } from './Analytics'
describe('Analytics Testing', () => {
beforeEach(() => {
expect(gaCodeExists).toHaveBeenCalledWith(true)
expect(trackEvent).toEqual(jasmine.any(Function))
})
it('track event was called without params', () => {
expect(trackEvent()).toBe(1); // replace 1 with desire output without params
})
it('track event was called with params', () => {
expect(trackEvent("somecategory", "somename", gaCodeExistsis)).toBe(1); // replace 1 with desire output with params
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.