加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:57:24 所属栏目:安全 来源:网络整理
导读:我理解错误的根源,我正在测试的组件需要将FormGroup传递给它的@Input()形式:FormGroup.在测试这个组件时,我无法弄清楚如何传递一个. 当我调用fixture.detectChanges()时,在我的每个函数之前出现错误,所以必须在该点之前传入表单 我当前的代码获取错误组未定
我理解错误的根源,我正在测试的组件需要将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]
    });
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读