我有一个功能,从谷歌分析跟踪分析。因此,在我的函数中,我需要查看代码是否存在。
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
}一开始我是这样做的(我很确定我做得不对)。
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);
}
});
})在阅读了一些博客文章并查看了stackover flow questions之后。我把它改为这样,我认为我是在模仿gaCodeExists变量,如下所示。
import constants from '../index';
import { isUsingGoogleAnalytics } from '../index';
describe('Analytics Testing', () => {
it('Should test trackEvent', () => {
constants.isUsingGoogleAnalytics = true;
expect(constants.isUsingGoogleAnalytics).toBe(true);
});
});现在我被困在如何在trackEvent函数上进行测试了。如何将模拟gaCodeExists变量应用到它?如果gaCodeExists是真的,那么测试用例就是expect(mockFunction).toHaveBeenCalled(1)。
P.S:我对测试很陌生。
发布于 2018-11-03 01:19:34
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
}首先,像这样修改您的分析代码,您的测试文件应该如下所示
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
})
});发布于 2018-11-02 03:24:16
在加载js文件时,gaCodeExists为变量分配值。而且trackEvent函数并不是纯的,因为它依赖于其作用域之外的值。让您的函数变得纯正是明智的,这样您就可以以正确的方式测试它。因为这是一个GA实现,所以您只需将gaCodeExists转换为一个函数,并在测试中对其进行模拟。
将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;
}将trackEvent函数更改为
function trackEvent(category = "Event", name) => {
if(gaCodeExists()) { // note this line
GoogleAnalytics.event({
category,
action: name,
});
}
return;
}现在可以通过模拟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
});
})我还没测试过这段代码。所以请再检查一遍它是否在做它想做的事情。
https://stackoverflow.com/questions/53111571
复制相似问题