嵌套服务的angular2单元测试

angular2 unit test of nested services

我正在尝试为以下服务编写一些单元测试:

export class Service1 {
...
    public constructor(service2: Service2, service3: Service3) {}
...
}

其中 Service2 依次包含 Service3:

export class Service2 {
...
    public constructor(service3: Service3, stringa: string) {}
...
}

我开始测试 以一种非常糟糕的方式,例如:

 it("check XXX", () => {
   let service3: Service3= new Service3();
   let service2: service2= new service2(service3, "stringa");
   let service1: Service1 = new Service1(service2, service3);
   expect(X).toEqual(X);
 });

但是现在,我正在使用 beforeEachProviders,这会发生什么:

  beforeEachProviders(() => {
    return [
      provide(Service2, { useclass: MockService2}),
      provide(Service3, { useclass: MockService3}),
      provide('stringa', { useValue: "stringa"}),
      Service1
    ];
  });

  it('check XXX ', inject(
  [Service2, Service3], (service2: Service2, service3: Service3) => {
    expect(true).toEqual(true);
  }));

是:

  Failed: No provider for String! (Service2-> String).

有人能帮帮我吗?

提前致谢。

如果你想从依赖注入中注入字符串值,你需要使用@Inject装饰器:

export class Service2 {
  ...
  public constructor(service3: Service3, @Inject('stringa') stringa: string) {}
  ...
}

事实上,如果不使用 @Inject 装饰器,注入默认依赖于参数类型。