将另一项服务注入服务以进行测试 - Angular2

时间:2016-07-14 07:17:28

标签: typescript angular angular2-services angular2-testing

我有两个服务,其中一个服务依赖于另一个服务:

UserService

@Injectable()
export class UserService {
  constructor(private apiService: ApiService){}
  ...
}

ApiService

@Injectable()
export class ApiService {
  constructor(){}
  ...
}

所以我的问题是我正在努力将依赖项ApiService注入UserService的测试中。这是我在用户服务规范中的内容。 (省略导入):

describe('UserService', () => {
  beforeEach(() => {
    addProviders([
      UserService,
      provide(ApiService, {useClass: MockApiService})
    ]);
  });

  it('Should ...', inject([UserService], (service: UserService) => {
    expect(true).toBe(true);
  }));
})

这引发的错误是:

Error: Cannot resolve all parameters for 'UserService'(undefined). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'UserService' is decorated with Injectable.

我还尝试在ApiService内的inject中添加it,但错误保持不变,因此: it('Should ..., inject([UserService, ApiService] ...))

带有TypeScript的Angular 2 RC4

0 个答案:

没有答案
相关问题