Ionic Karma/Jasmine测试离子6 -空进样器错误:没有地理位置提供程序

0md85ypi  于 2022-12-16  发布在  Ionic
关注(0)|答案(1)|浏览(98)

我在尝试将Ionic Native Geolocation作为依赖项进行测试时收到以下错误:

Failed: R3InjectorError(DynamicTestModule)[Geolocation -> Geolocation]:
          NullInjectorError: No provider for Geolocation!
        error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'Geolocation', 'Geolocation' ] })
        NullInjectorError: R3InjectorError(DynamicTestModule)[Geolocation -> Geolocation]:
          NullInjectorError: No provider for Geolocation!

测试设置如下所示:

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule, IonItem, Platform } from '@ionic/angular';
import { FoloLocation } from '../models/folo-location';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { HomePage } from './home.page';

describe('HomePage', () => {
  let component: HomePage;
  let fixture: ComponentFixture<HomePage>;
  let platform: Platform;

  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [ HomePage ],
      imports: [IonicModule.forRoot()],
      providers: [
        Geolocation
      ]
    }).compileComponents();

    fixture = TestBed.createComponent(HomePage);
    component = fixture.componentInstance;
    platform = TestBed.inject(Platform);
    fixture.detectChanges();
  }));

我也试过提供一个模拟,但无济于事。

ghg1uchk

ghg1uchk1#

显然从这个错误:

Failed: R3InjectorError(DynamicTestModule)[Geolocation -> Geolocation]:

看起来Geolocation依赖于Geolocation
你能试着做以下几件事吗?

providers: [
 { provide: Geolocation, useValue: {} }
],

我们为Geolocation服务提供了一个空对象。请查看您现在是否看到其他错误,如果看到,则必须模拟您使用的Geolocation的公共方法。

相关问题