[英]How to inject Ionic Geolocation in karma tests?
I'm doing my first non-hello-world app with ionic/angular.我正在用 ionic/angular 做我的第一个非 hello-world 应用程序。
I've created one page( map.page.ts
), which uses the Geolocation to center a map on the correct spot.我创建了一个页面(
map.page.ts
),它使用地理定位将 map 居中在正确的位置。 When I execute it, it works fine, but if I try to run the test, even the default one:当我执行它时,它工作正常,但如果我尝试运行测试,即使是默认测试:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { MapPage } from './map.page';
describe('MapPage', () => {
let component: MapPage;
let fixture: ComponentFixture<MapPage>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MapPage ],
imports: [IonicModule.forRoot()]
}).compileComponents();
fixture = TestBed.createComponent(MapPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
fails with this error:失败并出现此错误:
Failed: R3InjectorError(DynamicTestModule)[Geolocation -> Geolocation]: NullInjectorError: No provider for Geolocation!
Here is my map.page.ts:这是我的 map.page.ts:
import { Component, OnInit } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
@Component({
selector: 'app-map',
templateUrl: './map.page.html',
styleUrls: ['./map.page.scss'],
})
export class MapPage implements OnInit {
lat: number = 46.204391;
lng: number = 6.143158;
zoom: number = 15;
constructor(private geolocation: Geolocation) {}
ngOnInit() {
this.geolocation
.getCurrentPosition()
.then((resp) => {
this.lat = resp.coords.latitude;
this.lng = resp.coords.longitude;
})
.catch((error) => {
console.log('Error getting location', error);
});
}
}
And the Geolocation is declared in the map.module.ts: Geolocation 在 map.module.ts 中声明:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { MapPageRoutingModule } from './map-routing.module';
import { MapPage } from './map.page';
import { AgmCoreModule } from '@agm/core';
import { AgmJsMarkerClustererModule } from '@agm/js-marker-clusterer';
import { environment } from '../../../environments/environment';
import { Geolocation } from '@ionic-native/geolocation/ngx';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
MapPageRoutingModule,
AgmJsMarkerClustererModule,
AgmCoreModule.forRoot({
apiKey: environment.googleMapsAPIKey,
}),
],
declarations: [MapPage],
providers: [Geolocation],
})
export class MapPageModule {}
Why is karma not able to load this?为什么业力无法加载这个?
It belongs to MapPageModule
which wasn't imported into TestBed
.它属于未导入
MapPageModule
的TestBed
。 Because it's part of the same Component you're testing you can specify it separately in providers
section.因为它是您正在测试的同一组件的一部分,所以您可以在
providers
部分单独指定它。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { MapPage } from './map.page';
describe('MapPage', () => {
let component: MapPage;
let fixture: ComponentFixture<MapPage>;
beforeEach(async(() => {
const geolocation = {
getCurrentPosition: new Subject(),
};
TestBed.configureTestingModule({
declarations: [ MapPage ],
imports: [IonicModule.forRoot()],
providers: [
Geolocation, // <-- here, but better to mock.
// {
// providers: Geolocation,
// useValue: geolocation,
// },
],
}).compileComponents();
fixture = TestBed.createComponent(MapPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.