基本上我有一个看起来像这样的组件:
export class CmpB extends CmpA {
variableA:any = {};
@Input() config:any = {};
constructor(private serviceA: serviceA,
@Optional() private CmpC?: CmpC) {
super();
}
ngOnInit() {
if (this.CmpC) {
super.doSomething(parameterA);
//other stuff
}
}
}
所以基本上我得到了CmpB,它扩展了CmpA(根据配置改变了变量A)并且为了做某事它需要一个特定的父组件CmpC。 ......我需要为它编写一个实际验证CmpA突变的测试。
我的测试看起来像这样
describe('CmpB', () => {
let mock: any = MockComponent({ selector: "app-element", template: "<CmpC><CmpB></CmpB></CmpC>" });
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [mock, CmpC, CmpB],
providers: [
ServiceA
]
}).compileComponents();
});
it('CmpB component test', async(() => {
const appMock = TestBed.createComponent(mock);
appMock.detectChanges();
const el = appMock.debugElement.nativeElement as HTMLElement;
expect(el.tagName).toEqual("DIV");
}));
}
这是有效的,并且在覆盖范围方面它贯穿整个事情。 但我需要的是手动设置配置输入并查看variableA如何变异;我无法弄清楚如何。
模拟组件源自以下代码
import { Component } from '@angular/core';
export function MockComponent(options: Component): Component {
let metadata: Component = {
selector: options.selector,
template: options.template || '',
inputs: options.inputs,
outputs: options.outputs
};
class Mock {}
return Component(metadata)(Mock);
}