Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'Error: No value accessor for form control with name' in Angular Unit Test?

I fail to test a component that uses a custom component with ngModel

HTML code looks like this (see more in the repro below)

<custom-control name="formCode" [(ngModel)]="testValue"></custom-control>

The code is working in my app but it fails in my test with the following error

Uncaught Error: Uncaught (in promise): Error: No value accessor for form control with name: 'formCode'

Error: No value accessor for form control with name: 'formCode'

The tests are run with jasmine

I tried different imports but none seem to fix my issue

The repro code is here : https://stackblitz.com/edit/angular-test-ng-model

like image 623
bmtheo Avatar asked Jul 23 '19 15:07

bmtheo


People also ask

Should I create error no value accessor for form control with unspecified name attribute?

When you get the error No value accessor for form control with unspecified name attribute , there are generally two things that possibly has gone wrong: You are using ngModel with a third party control that doesn't register a NG_VALUE_ACCESSOR . In this case you need to use ngDefaultControl on the element.

What is control value accessor in angular?

Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.

What is value accessor for form control?

Control Value Accessor is an interface that provides us the power to leverage the Angular forms API and create a communication between Angular Form API and the DOM element. It provides us many facilities in angular like we can create custom controls or custom component with the help of control value accessor interface.

What is Ng_value_accessor?

NG_VALUE_ACCESSORlinkUsed to provide a ControlValueAccessor for form controls.


1 Answers

In my case, I was using a customised Angular Component (SingleSelectModule) that weren't imported in the Unit test file. I hope this could help anybody else that run into the same issue than me:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule } from '@angular/forms';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { CaselistComponent } from './caselist.component';
import { SingleSelectModule } from '@uimf/uitk/components/single-select';

describe('Component: CaselistComponent', () => {
  let component: CaselistComponent;
  let fixture: ComponentFixture<CaselistComponent>;

  beforeEach(async(() => {


    TestBed.configureTestingModule({
          declarations: [
            ...
          ],
          imports: [
            FormsModule,
            SingleSelectModule,
            RouterTestingModule,
            HttpClientTestingModule

          ],
          schemas: [CUSTOM_ELEMENTS_SCHEMA],

        }).compileComponents();

});

});
like image 121
vhbazan Avatar answered Sep 17 '22 21:09

vhbazan