微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何对实现ArcGIS API for Angular的类进行单元测试?

如何解决如何对实现ArcGIS API for Angular的类进行单元测试?

我正在使用esri-loader构建Angular 8应用程序,并且在尝试找出测试中模拟ArcGIS JS API依赖关系的最佳方法时,我真的很费劲。对于使用dojo以外的框架构建的应用程序,使用ArcGIS JS API的应用程序代码很少,其中还包含有意义的单元测试。 有没有人成功地做到这一点并能够提供一些指导?

例如,假设我有一个看起来像这样的MapComponent类:

export class MapComponent {
mapView: MapView;

public async initializeMap(lat: number,lon: number,zoom: number) {
const [Map,MapView] = await loadModules(['esri/Map','esri/views/MapView']);

this.mapView = new MapView({
   zoom: zoom,center: [lat,lon]
  map: new Map({
    basemap: 'streets'
  })
});
}

public async addPointToMap(lat: number,lon: number) {
const [Graphic] = await loadModules(['esri/Graphic']);
 
 const point = {
   type: "point",longitude: lon,latitude: lat
 };

 const markerSymbol = {
   type: "simple-marker",color: [226,119,40],outline: {
      color: [255,255,255],width: 2
   }
 };

 const pointGraphic = new Graphic({
   geometry: point,symbol: markerSymbol
 });
 
 this.mapView.graphics.add(pointGraphic);
 }
}

我想测试initializeMap()和addPointToMap()函数,但是在没有实际发出http请求以获取所需API模块并创建所需的每个类的具体实例的情况下,做到这一点的最佳方法是什么?一个测试类可能看起来像这样:

describe('MapComponent',() => {
let fixture: ComponentFixture<MapComponent>;
let component;

beforeEach(async () => {
 Testbed.configureTestingModule({
  declarations: [MapComponent],}).compileComponents();
 
fixture = Testbed.createComponent(MapComponent);
component = fixture.debugElement.componentInstance;
});

it('should initialize map',async () => {
 // calling this function will fetch the ArcGIS JS API dependencies
// and create concrete instances of Map and MapView objects - need
// to mock these      
await component.initializeMap();
expect(component.mapService.mapView).toBeTruthy();
});

it('should add a point to the map',async () => {
await component.addPointToMap(32.7,-117.1);
// assert point is created in component.mapService.mapView object
   });
 });

解决方法

Esri 社区帖子中的一位用户已经很好地回答了这个问题。这是链接:

https://community.esri.com/t5/arcgis-api-for-javascript/angular-developers-how-are-you-unit-testing-classes-which/m-p/1006825

想法是使用将 Arcgis 相关代码包装到包装器中,并使用第三方库 Typemoq 创建模拟对象并将它们传入和传出以验证功能。

这是一个有点老的问题,但如果他们不熟悉使用 Arcgis 测试模块,它会有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。