如何模拟 FormBuilder 以进行 Angular 组件单元测试

ine*_*now 3 testing formbuilder angular angular-reactive-forms angular9

我正在从事 Angular 9 项目。

我有一个组件接受 formBuilder 作为来自父组件的输入。这是我的子组件(我正在测试的组件):

export class ChildComponent implements OnInit {
  @Input() parentForm: FormGroup;  //this is coming from the parentComponent

  ngOnInit(): void {
    if (this.parentForm) {
      this.filteredSelectables = this.parentForm
        .get("recipientTypes")
        ...
    }
  }
...
Run Code Online (Sandbox Code Playgroud)

我想为此组件编写测试,但我需要创建一个测试可以使用的表单(或者我需要模拟父组件并返回我想要的表单?)

我已将 FormBuilder 添加到 testBed 提供程序中,但我仍然不知道如何制作可以测试的模拟表单。“应该创建”测试正在通过,但我无法测试其他任何内容,因为它们的parentForm 不能为空。这是我当前的测试:

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ChildComponent, MatAutocomplete],
      providers: [FormBuilder],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(ChildComponent);
        component = fixture.componentInstance;
      });
  }));

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

  it("should create", () => {
    expect(component).toBeTruthy();
  });
...
Run Code Online (Sandbox Code Playgroud)

我尝试创建这样的表单:

component.parentForm = FormBuilder.group({
      recipientTypes: new FormControl(
        {
          value: ["mock"],
          disabled: true
        },
        Validators.required
      )
    });
Run Code Online (Sandbox Code Playgroud)

并将其添加到 beforeEach() 甚至实际测试中。但我得到一个错误。

我想也许我需要模拟parentComponent并让它发送formBuilder?这是我的父组件:

export class ParentComponent implements OnInit, OnDestroy {
  parentForm: FormGroup;

  constructor(
    private router: Router,
    private formBuilder: FormBuilder
  ) {}

  ngOnInit() {
    this.setFormTemplate();
  }

  setFormTemplate() {
    this.templateForm = this.formBuilder.group({
      name: new FormControl(
        {
          value: this.name,
          disabled: true
        },
        Validators.required
      ),
      recipientTypes: new FormControl(
        {
          value: this.recipientTypes,
          disabled: true
        },
        Validators.required
      )
    });
  }
...
Run Code Online (Sandbox Code Playgroud)

如何为我的测试制作 formBuilder?

Ali*_*F50 9

尝试!:

import { FormBuilder } from '@angular/forms';
....
describe("ChildComponent", () => {
  let component: ChildComponent;
  let fixture: ComponentFixture<ChildComponent>;
  let formBuilder: FormBuilder;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ChildComponent, MatAutocomplete],
      providers: [FormBuilder], // add this as a provider
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(ChildComponent);
        component = fixture.componentInstance;
      });
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ChildComponent);
    component = fixture.componentInstance;
    formBuilder = TestBed.inject(FormBuilder); // get a handle on formBuilder
    // add the mock data here
    component.parentForm = formBuilder.group({ 
      recipientTypes: new FormControl(
        {
          value: ["mock"],
          disabled: true
        },
        Validators.required
      )
    });
    // this fixture.detectChanges will kick off the ngOnInit
    fixture.detectChanges();
  });

  it("should create", () => {
    expect(component).toBeTruthy();
  });
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试此操作时,出现错误:“类型‘typeof FormBuilder’上不存在属性‘组’”。 (2认同)