unit-testing - Angular 2 和 Jasmine : "Uncaught Error at injectionError"

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

我在服务测试中收到“注入(inject)错误时 Uncaught Error ”,但我不知道问题出在哪里。有谁知道缺少什么注入(inject)器以及我需要将其放在哪里?

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]

最佳答案

您仍然需要配置 WindowService 进行测试

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

关于unit-testing - Angular 2 和 Jasmine : "Uncaught Error at injectionError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43484829/

相关文章:

c++ - 使用 Google Mocks 模拟免费函数

python - 在 Django Rest Framework 中使用身份验证测试 POST

javascript - Angular 2 document.removeEventListener 在类里面不起作用

angular - 在 Angular Testing 中将表单控件设置为脏

javascript - 如何改变每次测试之间的 spy 结果

javascript - 测试JQuery的 "on"方法触发回调函数

javascript - 如何向 Jest Expect 添加自定义消息?

java - 使用 CouchBase-Lite 移动设备进行单元测试,无需 Android 应用程序上下文

javascript - Angular 4 index.html不会加载任何脚本

AngularJS单元测试: Constructor Test: Windows Azure Invoke Api