使用NgForm作为参数的打字稿方法的业力单元测试

时间:2019-04-19 09:26:35

标签: angular typescript karma-jasmine

我正在尝试为方法编写单元测试:

public addCred:boolean=true;
public credName:any;

public addMachineCredential(credentialForm: NgForm) {
    this.addCred = true;
    this.credName = credentialForm.value.name;
}

在我的测试课中:

  it('should add machine credential', () => {
    var machineCredentialForm: NgForm = new NgForm(null, null);
    machineCredentialForm.controls['name'].setValue('name');
    machineCredentialForm.controls['username'].setValue('username');
    machineCredentialForm.controls['password'].setValue('password');
    component.addMachineCredential(machineCredentialForm);
    expect(component.addCred).toBe(true);
    expect(component.credName).toBe("name");
  });

我遇到错误:TypeError: Cannot read property 'setValue' of undefined

如何测试功能“ addMachineCredential”?

1 个答案:

答案 0 :(得分:0)

为了能够对模块进行单元测试,您应该使用ReactiveForms而不是注释中提到的模板驱动的表单。我进行了重构(并根据您发布的摘录提供了一个示例...),提出可能的解决方案:

因此,这是重构代码以使其正常工作的一种方法。

您的组件:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-unit-test',
  template: `
  <p>Works</p>
  `,
  styleUrls: ['./unit-test.component.css']
})
export class UnitTestComponent {
  public addCred: boolean = true;
  public credName: any;

  form: FormGroup = new FormGroup({
    name: new FormControl('', [])
  });

  constructor() {}

  public addMachineCredential(credentialForm: FormGroup) {
    this.addCred = true;
    this.credName = credentialForm.controls.name.value;
  }
}

您的规格文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { UnitTestComponent } from './unit-test.component';

fdescribe('UnitTestComponent', () => {
  let component: UnitTestComponent;
  let fixture: ComponentFixture<UnitTestComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [UnitTestComponent],
      imports: [FormsModule, ReactiveFormsModule]
    }).compileComponents();
  }));

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

  it('should add machine credential', () => {
    component.form.controls.name.setValue('name');
    component.addMachineCredential(component.form);
    expect(component.addCred).toBe(true);
    expect(component.credName).toBe('name');
  });
});

别忘了您的组件模块必须声明ReactiveFormsModule,..

您的模块:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { UnitTestComponent } from './Questions/unit-test/unit-test.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    UnitTestComponent,
    FormsModule,
    ReactiveFormsModule
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

希望有帮助。