Angular 角度测试:从父组件访问子组件的元素

Angular 角度测试:从父组件访问子组件的元素,angular,unit-testing,jasmine,karma-jasmine,angular7,Angular,Unit Testing,Jasmine,Karma Jasmine,Angular7,我的父组件: html: parent.spec.ts: describe("ParentComponent", () => { let component: ParentComponent; let fixture: ComponentFixture<ParentComponent>; beforeEach(async(() => { TestBed.configureTestingModule({

我的父组件:
html:


parent.spec.ts:

describe("ParentComponent", () => {
      let component: ParentComponent;
      let fixture: ComponentFixture<ParentComponent>;

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [
            ParentComponent,
            ChildComponent
          ]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(ParentComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it("should create", () => {
        expect(component).toBeTruthy();
      });
    });
description(“ParentComponent”,()=>{
let组件:ParentComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[
父组件,
子组件
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ParentComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它(“应该创建”,()=>{
expect(component.toBeTruthy();
});
});
child.html:

<div [ngSwitch] = "errorType"> 
  <div *ngSwitchCase = "1">
    SomeError
  </div>
<p #note hidden="true"
</div>

一些错误

您应该使用
OnChanges
生命周期钩子来检测从父级到子级输入的任何更改:您可以在子组件中使用以下代码来获取最新输入元素的值:

ngOnChanges(changes: SimpleChanges) {

        let errorType: SimpleChange = changes.errorType.currentValue;
        console.log(errorType)

    }

您应该使用
OnChanges
生命周期钩子来检测从父级到子级输入的任何更改:您可以在子组件中使用以下代码来获取最新输入元素的值:

ngOnChanges(changes: SimpleChanges) {

        let errorType: SimpleChange = changes.errorType.currentValue;
        console.log(errorType)

    }

我通常更喜欢浅层测试方法和测试,即当子组件发出notes并传递errorType的正确属性时,父组件的行为符合预期,而不是再次整体测试子组件。但是如果您想以这种方式进行测试,则需要在父测试中模拟data.errorMsg.firstname,以便正确绑定errorType。你能不能在stackblitz中重现这个问题,这样就更容易向你展示不同的解决方案。@Erbsenkoenig坦率地说,我还没有决定任何具体的方法。我理解您建议的第二种方法,即初始化errorMsg和firstName变量,现在我已经开始了。测试用例成功。然而,我很难理解第一种方法——浅层测试。我必须重新测试组件,否则测试用例会失败。现在,当它被声明时,它的代码也被执行。我怎样才能只测试父级?为了给你一个实际的测试示例,请创建一个stackblitz,其中包含你想要测试的组件和你到目前为止得到的测试设置。否则,就不可能给你一个关于你的确切情况的完整例子。如果这不是一个选项,可以看看这里:我通常更喜欢浅层测试方法,并且测试,当子组件发出注释并且传递errorType的正确属性时,父组件的行为符合预期,而不是再次整体测试子组件。但是如果您想以这种方式进行测试,则需要在父测试中模拟data.errorMsg.firstname,以便正确绑定errorType。你能不能在stackblitz中重现这个问题,这样就更容易向你展示不同的解决方案。@Erbsenkoenig坦率地说,我还没有决定任何具体的方法。我理解您建议的第二种方法,即初始化errorMsg和firstName变量,现在我已经开始了。测试用例成功。然而,我很难理解第一种方法——浅层测试。我必须重新测试组件,否则测试用例会失败。现在,当它被声明时,它的代码也被执行。我怎样才能只测试父级?为了给你一个实际的测试示例,请创建一个stackblitz,其中包含你想要测试的组件和你到目前为止得到的测试设置。否则,就不可能给你一个关于你的确切情况的完整例子。如果这不是一个选项,也许可以看看这里:嘿@Amrit,现在,我不关心是否一切都很好。我的意思是我知道,但我不想测试错误是否正在撕裂等。我的动机是通过基本(应该创建)测试。嘿@Amrit,现在,我不关心是否一切正常。我的意思是我知道,但我不想测试错误是否正在渲染等。我的动机是通过基本(应该创建)测试。
ngOnChanges(changes: SimpleChanges) {

        let errorType: SimpleChange = changes.errorType.currentValue;
        console.log(errorType)

    }