小编pre*_*fly的帖子

单元测试使用带有路由器(和其他服务)的注入器的父类

假设我有一个类Foo为其所有派生类提供依赖注入服务,如下所示:

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);
}
Run Code Online (Sandbox Code Playgroud)

它具有从它派生的类:

export class Bar extends Foo {

    constructor(injector : Injector){
         super(injector);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何正确地对父类进行单元测试,而不会遇到:

Error: Can't resolve all parameters for Foo: (?)

目前我已经(尝试了很多很多不同的东西来让它工作,但失败了:()

export function main() {

    describe('Class: Foo', () => {
        beforeEach(async(() => {

            TestBed.configureTestingModule({
                providers: [ …
Run Code Online (Sandbox Code Playgroud)

angular2-di angular

2
推荐指数
1
解决办法
3466
查看次数

标签 统计

angular ×1

angular2-di ×1