Angular 2 beforeEachProviders在测试中使用Injector失败

时间:2016-03-14 18:12:51

标签: testing angular angular2-testing

当我尝试在测试中将Injector传递给beforeEachProviders时,我收到以下错误。

Failed: Cannot resolve all parameters for 'Injector'(?, ?, ?, ?, ?).
Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Injector' is decorated with Injectable.

我使用的代码

import { Injector } from 'angular2/core';

describe(() => {
  beforeEachProviders(() => [Injector]);
});

缺少什么?我应该给提供商什么能够实例化它?

1 个答案:

答案 0 :(得分:0)

我认为您不需要在供应商中配置Injector进行测试。这个类的一个实例可以直接注入你的测试......

以下是我在其中一个测试中所拥有的内容(没有Injector的“显式”提供程序),我可以在以后将其注入测试中:

import {it, describe, expect, beforeEach, inject, beforeEachProviders} from 'angular2/testing';
import {HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http';
import {MockBackend, MockConnection} from 'angular2/http/testing';
import {provide, Injector} from 'angular2/core';
import {HttpService} from "./http-service";

describe('HttpService Tests', () => {
  beforeEachProviders(() => {
    return [
      HTTP_PROVIDERS,
      provide(XHRBackend, { useClass: MockBackend }),
      HttpService
    ];
  });

  it('Should return a list of dogs', inject(
         [XHRBackend, HttpService, Injector],
         (mockBackend, httpService, injector) => {
    console.log(injector); // <----- Not null
  });
});