0

当我尝试传递InjectorbeforeEachProvidersin 测试时,我收到以下错误。

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]);
});

什么不见​​了?我应该给提供者什么才能实例化它?

4

1 回答 1

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
  });
});
于 2016-03-14T18:52:29.463 回答