繁体   English   中英

Karma/Jasmine 测试 Ionic 6 - NullInjectorError: No provider for Geolocation

[英]Karma/Jasmine testing Ionic 6 - NullInjectorError: No provider for Geolocation

尝试使用 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();
  }));

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

显然来自这个错误:

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

似乎Geolocation取决于Geolocation

您可以尝试执行以下操作:

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

我们为Geolocation服务提供了一个空的 object。 看看你现在是否看到了不同的错误,如果你看到了,你必须模拟你使用的Geolocation的公共方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM