【问题标题】:How do I inject GooglePlaceModule into Jasmine unit test如何将 GooglePlaceModule 注入 Jasmine 单元测试
【发布时间】:2023-02-16 21:30:14
【问题描述】:

我正在尝试为使用 GooglePlaceModule 的组件编写单元测试。我不断收到以下错误的失败测试:

Failed: Google maps library can not be found

在搜索解决方案时,我在网上尝试了 2 件事:

我根据建议在 karma.conf.js 文件中添加了这一行:

files: [
  'https://maps.googleapis.com/maps/api/js?key=<key>=places&language=enhttps://maps.google.com/maps/api/js?key=<removed>&libraries=places'
],

那没有用。

这是我的规格文件:

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { ProfileFormComponent } from './profile-form.component';
import { ReactiveFormsModule } from '@angular/forms';
import { GooglePlaceModule } from 'ngx-google-places-autocomplete';

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

  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [ ProfileFormComponent ],
      imports: [IonicModule.forRoot(), ReactiveFormsModule, GooglePlaceModule]
    }).compileComponents();

    window['google'] = {
      maps: {
        Maps: () => {}
      }
    };
    fixture = TestBed.createComponent(ProfileFormComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

到目前为止这是非常基本的,我只是想让最小的通过。

【问题讨论】:

  • 嗨@D.Hodges,我和你的情况相似。你能解决这个问题吗?

标签: javascript unit-testing karma-jasmine google-places-api


【解决方案1】:

我加了

 imports: [GoogleMapsModule]

按照我的规格。

来自 here 的 google-maps-api.js。将其添加到目录 src/app/pages/address-form/mock/

为了在我的测试中使用 google-maps-api.js 文件,我添加到 karma.conf.js

 files: ['src/app/pages/address-form/mock/google-maps-api.js']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多