角度测试平台overrideModule不起作用

jen*_*ent 5 testbed angular angular-test

当将以下配置用于测试治具时,我会抱怨找不到该标签。MockSelectionToolComponent直接替换in AppModule可以很好地工作,因此还必须做其他事情...

 // Add the imported module to the imports array in beforeEach
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [MockSelectionToolComponent],
            imports: [

                AppModule
            ]
        }).overrideModule(AppModule, {
            remove: {
                declarations: [SelectionToolComponent]
            }
        }).compileComponents();

        fixture = TestBed.createComponent(MappingComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        component.initialiseMap();
    });
Run Code Online (Sandbox Code Playgroud)

错误:模板解析错误:“ app-selection-tool”不是已知元素:

jen*_*ent 7

因此,实际上我们不是将其添加到测试模块的声明中,而是添加到原始模块中:

// Add the imported module to the imports array in beforeEach
beforeEach(() => {
    TestBed.configureTestingModule({
        declarations: [],
        imports: [

            AppModule
        ]
    }).overrideModule(AppModule, {
        remove: {
                declarations: [SelectionToolComponent]
            },
        add: {
                declarations: [MockSelectionToolComponent]
        }
    }).compileComponents();

    fixture = TestBed.createComponent(MappingComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.initialiseMap();
});
Run Code Online (Sandbox Code Playgroud)

祝您好运!