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
It is because you are mocking your CustomControlComponent in your test. Install @angular/material and its dependencies in your package.json and use the spec file below. The test will pass.
https://stackblitz.com/edit/angular-test-ng-model-vsk5re
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule, NG_VALUE_ACCESSOR, DefaultValueAccessor, ControlValueAccessor } from '@angular/forms';
// import { MockComponent } from 'ng2-mock-component';
import{MatFormFieldModule, MatInputModule} from '@angular/material';
import { ParentControlComponent } from './parent-control';
import {CustomControlComponent} from '../custom-control/custom-control';
describe('ParentControlComponent', () => {
let component: ParentControlComponent;
let fixture: ComponentFixture<ParentControlComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
CustomControlComponent,
ParentControlComponent,
// MockComponent({ selector: 'custom-control' }),
],
imports: [
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatFormFieldModule,
MatInputModule
],
providers: [
]
})
.compileComponents();
fixture = TestBed.createComponent(ParentControlComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});