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
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.
Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.
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.
NG_VALUE_ACCESSORlinkUsed to provide a ControlValueAccessor for form controls.
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();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With