Angular 2& Jasmine:"注射时出现未捕获错误"

时间:2017-04-19 01:36:11

标签: unit-testing angular jasmine karma-jasmine angular2-services

我得到了这个"在注射错误"在我的服务测试中,但我不知道问题出在哪里。有谁知道什么注射器丢失了,我需要把它放在哪里?

window.service.ts

import { InjectionToken } from '@angular/core';
export const WindowService = new InjectionToken('WindowService');

config.service.ts

import { Injectable, Inject } from '@angular/core';
import { WindowService } from './window.service';

@Injectable()
export class ConfigService {
  properties: any;

  constructor(@Inject(WindowService) private _Window: any) {

    this.properties = {
      APP_BASEURL: _Window.properties['app.baseurl'],
      SERVICE_BASEURL: _Window.properties['service.baseurl']
    };
  }
}

global.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { ConfigService } from '../shared/config.service';

@Injectable()
export class GlobalService {
    http: Http;
    headers: Headers;
    config: any;
    apiRoot: string;

    constructor(http: Http, config: ConfigService) {
        this.config = config.properties;
        this.http = http;
        this.headers = new Headers();
        this.headers.append('Content-Type', 'application/json');
        this.apiRoot = this.config.SERVICE_BASEURL + 'service/';
    }
}

global.service.spec.ts

import { TestBed, inject } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { HttpModule, Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod } from '@angular/http';

import { GlobalService } from './global.service';
import { ConfigService } from '../shared/config.service';

describe('GlobalService', () => {
  let mockBackend: MockBackend;
  let globalService: GlobalService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ HttpModule ],
      providers: [
        GlobalService,
        MockBackend,
        ConfigService,
        BaseRequestOptions,
        {
          provide: Http,
          useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(backend, options),
          deps: [ MockBackend, BaseRequestOptions ]
        }
      ]
    });
  });

  beforeEach(inject([ MockBackend, Http, ConfigService ],
    (mb: MockBackend, http: Http, config: ConfigService) => {
      mockBackend = mb;
      globalService = new GlobalService(http, config);
    }));

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

错误

FAILED GlobalService should ...

zone.js:169 Uncaught Error
    at injectionError (http://localhost:9877/base/src/test.ts:2267:86) [ProxyZone]
    at noProviderError (http://localhost:9877/base/src/test.ts:2305:12) [ProxyZone]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._throwOrNull (http://localhost:9877/base/src/test.ts:3806:19) [ProxyZone]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._getByKeyDefault (http://localhost:9877/base/src/test.ts:3845:25) [ProxyZone]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._getByKey (http://localhost:9877/base/src/test.ts:3777:25) [ProxyZone]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_.get (http://localhost:9877/base/src/test.ts:3646:21) [ProxyZone]
    at DynamicTestModuleInjector.get (ng:///DynamicTestModule/module.ngfactory.js:150:107) [ProxyZone]
    at DynamicTestModuleInjector.getInternal (ng:///DynamicTestModule/module.ngfactory.js:223:53) [ProxyZone]
    at DynamicTestModuleInjector.Array.concat.NgModuleInjector.get (http://localhost:9877/base/src/test.ts:4592:44) [ProxyZone]
    at TestBed.Array.concat.TestBed.get (http://localhost:9877/base/src/test.ts:16282:47) [ProxyZone]
    at http://localhost:9877/base/src/test.ts:16288:61 [ProxyZone]
    at Array.map (native) [ProxyZone]
    at TestBed.Array.concat.TestBed.execute (http://localhost:9877/base/src/test.ts:16288:29) [ProxyZone]
    at Object.<anonymous> (http://localhost:9877/base/src/test.ts:16378:45) [ProxyZone]

1 个答案:

答案 0 :(得分:3)

您仍然需要为测试配置WindowService

provider: [
  {
    provide: WindowService, useValue: whatEverIsToBeUsedAs_Window }
  }
]