reactjs - 使用笑话和酶对传单进行单元测试时未定义的 layerGroup()
问题描述
我正在尝试测试我MyCustomMap
是否正在调用我MapController
并创建一个新的传单地图组件。
但是MapController
设置_mapGroup
为组件undefined
,MyCustomMap
它应该是L.layerGroup()
(不是undefined
)。
我已经尝试过以不同的方式进行模拟,但MapController
仍然是. 怎么了?我该如何解决这个问题?leaflet
_mapGroup
undefined
我的 MyCustomMap 测试文件(custom-map.test.js):
import MyCustomMap from './custom-map';
import initialMapOptions from './map-options';
import MapController from './map-controller';
const mapCreateGroupMock = jest.fn(),
mapAddGroupToMap = jest.fn();
let myMap = null,
mapController = null;
beforeAll(() => {
const mapOptions = initialMapOptions();
mapController = {
createGroup: mapCreateGroupMock,
addGroupToMap: mapAddGroupToMap
};
myMap = new MyCustomMap(mapController, mapOptions);
});
describe('My custom map', () => {
it(`should call MapController and create a new leaflet map component`, () => {
expect(mapCreateGroupMock).toBeCalledTimes(1);
expect(mapAddGroupToMap).toBeCalledTimes(1);
expect(myMap._mapGroup).not.toBeNull(); // -> here is failing because myMap._mapGroup is undefined and shouldn't be
});
});
我的地图控制器(map-controller.js):
import L from 'leaflet';
class MapController {
constructor(container, configuration) {
this._map = new L.Map(container, { zoomControl: false, minZoom: this._minZoom });
this._container = document.getElementById(container);
//more code here ...
}
createGroup() {
return L.layerGroup();
}
addGroupToMap(group) {
this._map.addLayer(group);
}
//more code here ...
}
export default MapController;
我的 MyCustomMap 组件(custom-map.js):
class MyCustomMap {
constructor(mapController, configuration) {
this._mapController = mapController;
this._configuration = configuration;
this._mapGroup = this._mapController.createGroup();
this._mapController.addGroupToMap(this._mapGroup);
//more code here ...
}
}
解决方案
解决了!我mapCreateGroupMock
只是返回一个空函数,我需要模拟一个返回值。所以我:
- 嘲笑那个价值;
mockClear()
对我的模拟函数执行 a ;- 做了每一个步骤
beforeEach()
; - 并将我的
myMap._mapGroup
断言更改.toBeTruthy()
为检查是否未定义且不为空。
现在它按预期工作。
MyCustomMap 测试文件 (custom-map.test.js) 的最终更改:
import MyCustomMap from './custom-map';
import initialMapOptions from './map-options';
const mapCreateGroupMock = jest.fn(),
mapAddGroupToMap = jest.fn(),
mapOptions = initialMapOptions();
let myMap = null,
mapController = null;
describe('My custom map', () => {
beforeEach(() => {
mapCreateGroupMock.mockClear();
mapAddGroupToMap.mockClear();
const addLayerMock = jest.fn(),
mapGroup = {
addLayer: addLayerMock
};
mapCreateGroupMock.mockReturnValueOnce(mapGroup);
mapController = {
createGroup: mapCreateGroupMock,
addGroupToMap: mapAddGroupToMap
};
myMap = new MyCustomMap(mapController, mapOptions);
});
it(`should call MapController and create a new leaflet map component`, () => {
expect(mapCreateGroupMock).toBeCalledTimes(1);
expect(mapAddGroupToMap).toBeCalledTimes(1);
expect(myMap._mapGroup).toBeTruthy();
});
});
推荐阅读
- three.js - 非常远的点光源无法正确渲染
- angular - 嵌套路由:如何在路由内路由?
- java - 匹配位,然后读取 16 位并存储到新变量
- r - 使用全局环境中的数据调试 rmd 文件
- go - 为什么 Go 将值 0 返回给未分配的整数变量?
- java - Android-Java 列表到字符串,反之亦然
- laravel - 运行内置授权中间件时出现异常
- amazon-web-services - 负载均衡器 cookie 粘性和用于存储用户会话的 ElastiCache 有什么区别?
- multithreading - 所有线程都在 RUNABLE 状态下阻塞 10 小时
- .net - Linux上的.net核心控制台应用程序-要执行的应用程序不存在