Unit testing 角度2单元测试组件与其他组件依赖关系
基本上,我有一个如下所示的组件:Unit testing 角度2单元测试组件与其他组件依赖关系,unit-testing,angular,angular2-testing,Unit Testing,Angular,Angular2 Testing,基本上,我有一个如下所示的组件: export class CmpB extends CmpA { variableA:any = {}; @Input() config:any = {}; constructor(private serviceA: serviceA, @Optional() private CmpC?: CmpC) { super(); } ngOnInit() { if (this.Cm
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(根据配置的不同而变化),为了做一些事情,它需要一个特定的父组件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");
}));
}
+如果可以的话,一百万。奇怪的是,这些文件中没有关于如何做到这一点的内容……你解决了吗?@JanatbekSharsheyev no我因为其他优先事项而放弃了,但它仍然在我的待办事项清单上:)
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);
}