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