使用JS库的Angular2 Karma测试返回ReferenceError

时间:2016-10-08 11:15:00

标签: angular typescript karma-runner angular-cli

我在我的Angular-Cli项目中包含了Google API JS库,并安装了这些类型,因此我在编码时没有遇到任何问题。但是当我想测试服务时,我得到ReferenceError: gapi is not defined错误

我没有在构造函数中传递它,它是一个全局函数

private checkAuth() {
    gapi.auth.authorize({
      client_id: calendarApi,
      scope: 'https://www.googleapis.com/auth/calendar.readonly',
      immediate: true,
    }, this.next())};

测试文件:

import { TestBed, inject } from '@angular/core/testing';
import { CalendarService } from './calendar.service';

describe('Service: Calendar', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [CalendarService]
    });
  });

  it('should ...', inject([CalendarService], (service: CalendarService) => {
    expect(service).toBeTruthy();
  }));
});

gapi来自此文件https://apis.google.com/js/client.js

所以我必须在Test文件中包含一些内容,还是必须配置angular-cli来识别引用?

Eidt: 我将文件包含在karma.conf.js文件中。现在他找到了变量但没有找到它的方法。

0 个答案:

没有答案