【发布时间】:2020-10-30 00:16:26
【问题描述】:
有没有办法为每个测试更改组件html模板?
我在单元测试中创建了一个组件,我希望该组件的 html 模板在其他测试中有所不同。
我尝试使用 TestBed.overrideComponent (如代码所示)但出现错误:
“当测试模块已经被实例化时,不能覆盖组件元数据。”
@Component({
template: `<div> <span *directive="let letter from ['x','y','z']"> </span> </div>`
})
class TestComponent {
isEnabled: any;
}
describe('Directive', () => {
let fixture: ComponentFixture<TestComponent>;
let element: DebugElement;
let component: TestComponent;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent, Directive],
});
});
it('should do something with the initial component template', () =>{ ... });
it('should do something with another template', () => {
fixture = TestBed.overrideComponent(TestComponent, {
set: {
template: `<div> <span *directive="let letter from ['a','b','c'] enabled:isEnabled "> </span> </div>`
}
})
.createComponent(TestComponent);
component = fixture.componentInstance;
component.isEnabled = true;
fixture.detectChanges();
...
});
});
【问题讨论】:
标签: angular unit-testing