我有两个服务,其中一个服务依赖于另一个服务:
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