Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
No value accessor while trying to test basic form with angular 4 and ionic 3

I have a simple ionic form:

<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>

and a page component:

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);
    }
}

and a test:

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();
        });
    });
});

project and test structure is based on https://github.com/marcoturi/ionic-boilerplate. The test fails due to error: No value accessor for form control.

I was trying various approaches yet it seems that combination of Ionic specifics and recent Angular 4 makes it requiring some additional configuration to be added and some of the existing solutions are no longer valid due to changes in Angular.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I hit this problem two days ago and thanks to isolating it to post on SO was able to get to the solution base on slightly similar question

Main point is to add IonicModule to imports yet this triggers bunch of other Ionic imports:

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

and so updated beforeEach should start with

    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 with the above example got already merged to https://github.com/marcoturi/ionic-boilerplate.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda