gpt4 book ai didi

javascript - 尝试使用 angular 4 和 ionic 3 测试基本形式时没有值访问器

转载 作者:太空狗 更新时间:2023-10-29 19:29:20 25 4
gpt4 key购买 nike

我有一个简单的 ionic 形式:

<ion-header>
<ion-navbar>
<ion-title>Foo</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<form [formGroup]="fooForm">
<ion-item>
<ion-label floating>Name</ion-label>
<ion-input formControlName="name" type="text"></ion-input>
</ion-item>
</form>
<button ion-button full color="primary" (click)="save()">Save</button>
</ion-content>

和一个页面组件:

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

@Component({
selector: 'fo-page-foo',
templateUrl: './foo.html'
})
export class FooPage {
fooForm: FormGroup;

constructor(public formBuilder: FormBuilder) {
}

ngOnInit() {
this.fooForm = this.formBuilder.group({
name: ['']
});

}

public save() {
console.log(this.fooForm.value);
}
}

和一个测试:

import { FooPage } from './foo';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { Form } from 'ionic-angular';

describe('Foo Page:', () => {

let comp: FooPage;
let fixture: ComponentFixture<FooPage>;
let de: DebugElement;

beforeEach(() => {
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
declarations: [FooPage],
providers: [Form],
imports: [FormsModule, ReactiveFormsModule],
});
fixture = TestBed.createComponent(FooPage);
fixture.detectChanges();
comp = fixture.componentInstance;
de = fixture.debugElement;
comp.ngOnInit();
});

describe('.constructor()', () => {
it('Should be defined', () => {
expect(comp).toBeDefined();
});
});
});

项目和测试结构基于https://github.com/marcoturi/ionic-boilerplate .由于错误,测试失败:No value accessor for form control

我正在尝试各种方法,但似乎 Ionic 细节和最近的 Angular 4 的组合使得它需要添加一些额外的配置,并且由于 Angular 的变化,一些现有的解决方案不再有效。

最佳答案

我两天前遇到了这个问题,感谢将其隔离并发布在 SO 上,因此能够找到基于 slightly similar question 的解决方案

要点是将 IonicModule 添加到 imports 但这会触发一堆其他 Ionic 导入:

import {
App, Platform, Config, Keyboard,
Form, IonicModule, DomController
} from 'ionic-angular';
import {
mockConfig, mockDomController, MockPlatform, mockPlatform
} from 'ionic-angular/util/mock-providers';

因此更新后的beforeEach应该以

开头
    TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
declarations: [FooPage],
providers: [
App,
Form,
Keyboard,
{ provide: Platform, useClass: MockPlatform },
{ provide: DomController, useValue: mockDomController(mockPlatform()) },
{ provide: Config, useValue: mockConfig() },
],
imports: [
FormsModule,
IonicModule,
ReactiveFormsModule
],
});

上述示例的 PR 已经合并到 https://github.com/marcoturi/ionic-boilerplate .

关于javascript - 尝试使用 angular 4 和 ionic 3 测试基本形式时没有值访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com