单元测试将 Injector 与 Router(和其他服务)一起使用的父 class
Unit testing a parent class that uses Injector with Router (and other services)
假设我有 class Foo
,它为其所有派生的 classes 提供依赖注入服务,例如:
export class Foo {
protected fb : FormBuilder;
protected router : Router;
protected otherService : OtherService;
protected anotherService : AnotherService;
constructor(injector : Injector) {
this.fb = injector.get(FormBuilder);
this.router = injector.get(Router);
this.otherService = injector.get(OtherService);
this.anotherService = injector.get(AnotherService);
}
其中有 class 派生自它:
export class Bar extends Foo {
constructor(injector : Injector){
super(injector);
}
}
如何在没有 运行 的情况下正确地对父 class 进行单元测试:
Error: Can't resolve all parameters for Foo: (?)
此刻我已经(尝试了很多,很多不同的方法来让它工作,但失败了:( )
export function main() {
describe('Class: Foo', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Foo,
Injector,
OtherService,
AnotherService
]
});
}));
it('should compile', inject([Foo, Injector], ( foo: Foo, injector : Injector ) => {
console.log("INJECTOR", injector);
expect(foo).toBeTruthy();
}));
});
}
我也尝试过这样使用 ReflectiveInjector.resolveAndCreate
:
{
provide : Injector,
useValue: ReflectiveInjector.resolveAndCreate([FormBuilder, Router, OtherService, AnotherService])
}
但仍然没有骰子:(有什么想法吗?
似乎您需要在 Foo
构造函数
中为 Injector
添加 @Inject
装饰器
constructor(@Inject(Injector) injector: Injector) {
并正确配置 TestBed
。更准确地说,你必须导入 RouterModule
否则你会遇到 Router
:
的另一个问题
TestBed.configureTestingModule({
imports: [
RouterModule.forRoot([])
],
providers: [
FormBuilder,
Foo,
OtherService,
AnotherService
]
});
您可以在 Plunker Example
中试用
假设我有 class Foo
,它为其所有派生的 classes 提供依赖注入服务,例如:
export class Foo {
protected fb : FormBuilder;
protected router : Router;
protected otherService : OtherService;
protected anotherService : AnotherService;
constructor(injector : Injector) {
this.fb = injector.get(FormBuilder);
this.router = injector.get(Router);
this.otherService = injector.get(OtherService);
this.anotherService = injector.get(AnotherService);
}
其中有 class 派生自它:
export class Bar extends Foo {
constructor(injector : Injector){
super(injector);
}
}
如何在没有 运行 的情况下正确地对父 class 进行单元测试:
Error: Can't resolve all parameters for Foo: (?)
此刻我已经(尝试了很多,很多不同的方法来让它工作,但失败了:( )
export function main() {
describe('Class: Foo', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Foo,
Injector,
OtherService,
AnotherService
]
});
}));
it('should compile', inject([Foo, Injector], ( foo: Foo, injector : Injector ) => {
console.log("INJECTOR", injector);
expect(foo).toBeTruthy();
}));
});
}
我也尝试过这样使用 ReflectiveInjector.resolveAndCreate
:
{
provide : Injector,
useValue: ReflectiveInjector.resolveAndCreate([FormBuilder, Router, OtherService, AnotherService])
}
但仍然没有骰子:(有什么想法吗?
似乎您需要在 Foo
构造函数
Injector
添加 @Inject
装饰器
constructor(@Inject(Injector) injector: Injector) {
并正确配置 TestBed
。更准确地说,你必须导入 RouterModule
否则你会遇到 Router
:
TestBed.configureTestingModule({
imports: [
RouterModule.forRoot([])
],
providers: [
FormBuilder,
Foo,
OtherService,
AnotherService
]
});
您可以在 Plunker Example
中试用