Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 角度2&;茉莉花:“;injectionError处的未捕获错误“;_Unit Testing_Angular_Jasmine_Karma Jasmine_Angular2 Services - Fatal编程技术网

Unit testing 角度2&;茉莉花:“;injectionError处的未捕获错误“;

Unit testing 角度2&;茉莉花:“;injectionError处的未捕获错误“;,unit-testing,angular,jasmine,karma-jasmine,angular2-services,Unit Testing,Angular,Jasmine,Karma Jasmine,Angular2 Services,我在服务测试中遇到了“injectionError的未捕获错误”,但我不知道问题出在哪里。有人知道什么注射器丢失了,我需要把它放在哪里吗 window.service.ts import { InjectionToken } from '@angular/core'; export const WindowService = new InjectionToken('WindowService'); import { Injectable, Inject } from '@angular/cor

我在服务测试中遇到了“injectionError的未捕获错误”,但我不知道问题出在哪里。有人知道什么注射器丢失了,我需要把它放在哪里吗

window.service.ts

import { InjectionToken } from '@angular/core';
export const WindowService = new InjectionToken('WindowService');
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']
    };
  }
}
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/';
    }
}
config.service.ts

import { InjectionToken } from '@angular/core';
export const WindowService = new InjectionToken('WindowService');
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']
    };
  }
}
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.ts

import { InjectionToken } from '@angular/core';
export const WindowService = new InjectionToken('WindowService');
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']
    };
  }
}
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();
  }));
}
错误

失败的GlobalService应该。。。
zone.js:169未捕获错误
注射时错误(http://localhost:9877/base/src/test.ts:2267:86)[丙氧酮]
在没有提供错误(http://localhost:9877/base/src/test.ts:2305:12)[丙氧酮]
在ReflectiveInjector_u.Array.concat.ReflectiveInjector_u.\u0.null处(http://localhost:9877/base/src/test.ts:3806:19)[丙氧酮]
在ReflectiveInjector_u.Array.concat.ReflectiveInjector_u.\u getByKeyDefault(http://localhost:9877/base/src/test.ts:3845:25)[丙氧酮]
在ReflectiveInjector_u.Array.concat.ReflectiveInjector_u.\u getByKey(http://localhost:9877/base/src/test.ts:3777:25)[丙氧酮]
在ReflectiveInjector_u979;.Array.concat.ReflectiveInjector_979;.get(http://localhost:9877/base/src/test.ts:3646:21)[丙氧酮]
在DynamicTestModuleInjector.get(ng:///DynamicTestModule/module.ngfactory.js:150:107)[ProxyZone]
在DynamicTestModuleInjector.getInternal(ng:///DynamicTestModule/module.ngfactory.js:223:53)[ProxyZone]
在DynamicTestModuleInjector.Array.concat.NgModuleInjector.get(http://localhost:9877/base/src/test.ts:4592:44)[丙氧酮]
在TestBed.Array.concat.TestBed.get(http://localhost:9877/base/src/test.ts:16282:47)[丙氧酮]
在http://localhost:9877/base/src/test.ts:16288:61 [丙氧酮]
at Array.map(本机)[ProxyZone]
在TestBed.Array.concat.TestBed.execute(http://localhost:9877/base/src/test.ts:16288:29)[丙氧酮]
反对。(http://localhost:9877/base/src/test.ts:16378:45)[丙氧酮]

您仍然需要为测试配置
WindowsService

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