Karma formGroup 需要一个 FormGroup 实例。请传一个进来
Karma formGroup expects a FormGroup instance. Please pass one in
我了解错误的根源,我正在测试的组件需要将 FormGroup
传递到它的 @Input() form: 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, ComponentFixture, 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],
ip: [
'',
Validators.required
],
action: ['action', 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],
ip: [
'',
Validators.required
],
action: ['action', Validators.required]
});
}
}
我运行进入运行detectChanges
后同样的错误。但是,我的组件的 FormGroup 是在 ngOnInit 中启动的,而不是作为输入传递的。
我的案例的解决方案是将 component under test
包装在包装器组件中。此过程将强制 Angular 将 component under test
置于生命周期事件中,例如ngOnInit,否则您将不得不自己调用。它也感觉像是用输入测试组件的正确方法。它有点样板,但这种方法更接近地模仿 angular 的自然行为。
这是此 Medium article
的代码复制
describe('ComponentUnderTestComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ComponentUnderTestComponent, TestHostComponent]
}).compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
});
it('should show TEST INPUT', () => {
expect(testHostFixture.nativeElement.querySelector('div').innerText)
.toEqual('TEST INPUT');
});
@Component({
selector: `host-component`,
template: `<component-under-test input="test input"></component-under-test>`
})
class TestHostComponent {
}
});
我了解错误的根源,我正在测试的组件需要将 FormGroup
传递到它的 @Input() form: 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, ComponentFixture, 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],
ip: [
'',
Validators.required
],
action: ['action', 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],
ip: [
'',
Validators.required
],
action: ['action', Validators.required]
});
}
}
我运行进入运行detectChanges
后同样的错误。但是,我的组件的 FormGroup 是在 ngOnInit 中启动的,而不是作为输入传递的。
我的案例的解决方案是将 component under test
包装在包装器组件中。此过程将强制 Angular 将 component under test
置于生命周期事件中,例如ngOnInit,否则您将不得不自己调用。它也感觉像是用输入测试组件的正确方法。它有点样板,但这种方法更接近地模仿 angular 的自然行为。
这是此 Medium article
的代码复制 describe('ComponentUnderTestComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ComponentUnderTestComponent, TestHostComponent]
}).compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
});
it('should show TEST INPUT', () => {
expect(testHostFixture.nativeElement.querySelector('div').innerText)
.toEqual('TEST INPUT');
});
@Component({
selector: `host-component`,
template: `<component-under-test input="test input"></component-under-test>`
})
class TestHostComponent {
}
});