如何模拟用于角度组件单元测试的FormBuilder

时间:2020-03-06 15:33:50

标签: angular testing angular-reactive-forms formbuilder angular9

我正在Angular 9项目中工作。

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

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")
        ...
    }
  }
...

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

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

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

我尝试过创建这样的表单:

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

并将其添加到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
      )
    });
  }
...

如何为测试创建formBuilder?

1 个答案:

答案 0 :(得分:1)

尝试:

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.get(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();
  });