Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 – Karma formGroup需要一个FormGroup实例.请通过一个全部内容,希望文章能够帮你解决angular – Karma formGroup需要一个FormGroup实例.请通过一个所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: