Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 角度2单元测试组件与其他组件依赖关系_Unit Testing_Angular_Angular2 Testing - Fatal编程技术网

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);
}