angular – Karma formGroup需要一个FormGroup实例.请通过一个

我理解错误的根源,我正在测试的组件需要将FormGroup传递给它的@input()形式:FormGroup.在测试这个组件时,我无法弄清楚如何传递一个.

当我调用fixture.detectChanges()时,在我的每个函数之前出现错误,所以必须在该点之前传入表单

我当前的代码获取错误组未定义:

import { async,ComponentFixture,Testbed } from '@angular/core/testing';
import {
    ReactiveFormsModule,FormsModule,Validators,FormBuilder
} from '@angular/forms';
import { StaticComponent } from '../../firewall/static/static.component';

describe('StaticComponent',() => {
    let component: StaticComponent;
    let fixture: ComponentFixture<StaticComponent>;

    beforeEach(
        async(() => {
            Testbed.configureTestingModule({
                declarations: [
                    StaticComponent
                ],imports: [
                    CommonModule,ReactiveFormsModule,FormsModule
                ],providers: [
                    NetworkService,NetworkValidator,HostNameValidator,NotificationsService
                ]
            }).compileComponents();
        })
    );

    beforeEach(() => {
        fixture = Testbed.createComponent(StaticComponent);
        component = fixture.componentInstance;
        component.ruleForm = FormBuilder.group({
            chain: ['chain',Validators.required],ip: [
                '',Validators.required,this.networkValidator.validateNetwork('ip')
            ],action: ['action',Validators.required]
        });
        fixture.detectChanges();
    });

    fit('should be created',() => {
        expect(component).toBeTruthy();
    });
});

如何在测试期间将预制表单传递给组件的@Input?我似乎无法正确提供FormBuilder

解决方法

这是我为您提出的测试组件规范.请注意我添加的模拟FormBuilder以及我在规范中提供它的方式.

import { async,Testbed } from '@angular/core/testing';
import { TestingComponent } from './testing.component';
import { FormBuilder,Validators } from '@angular/forms';

describe('TestingComponent',() => {
  let component: TestingComponent;
  let fixture: ComponentFixture<TestingComponent>;
  const formBuilder: FormBuilder = new FormBuilder();

  beforeEach(async(() => {
    Testbed.configureTestingModule({
      declarations: [ TestingComponent ],providers: [ { provide: FormBuilder,useValue: formBuilder } ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = Testbed.createComponent(TestingComponent);
    component = fixture.componentInstance;
    component.ruleForm = formBuilder.group({
      chain: ['chain',Validators.required
            ],Validators.required]
    });
    fixture.detectChanges();
  });

  it('should create',() => {
    expect(component).toBeTruthy();
  });
});

这是我的测试组件,以防您需要参考.

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

@Component({
  selector: 'app-testing',templateUrl: './testing.component.html',styleUrls: ['./testing.component.css']
})
export class TestingComponent implements OnInit {
  ruleForm: FormGroup = new FormGroup({});
  constructor(private formBuilder: FormBuilder) { }

  ngOnInit() {
    this.ruleForm = this.formBuilder.group({
      chain: ['chain',Validators.required]
    });
  }
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...