I need some guidance on creating unit tests for a Custom Form Control in Angular 9. The issue arises with this line of code:
constructor(@Self() private ngControl: NgControl)
, which triggers an error: Error: NodeInjector: NOT_FOUND [NgControl]
. It seems that I must set a value accessor somehow.
I have injected ngControl
in the constructor because I will be using it later in my component. This is why I did not opt for the typical implementation with provide: NG_VALUE_ACCESSOR
.
I attempted to mock it like so:
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: NgControl, useValue: new FormControlDirective([], [], [], null) }
],
imports: [FormsModule, ReactiveFormsModule]
}).compileComponents();
However, I encountered the following error:
No valid value accessor for form control with unspecified name attribute
since I am unsure of what should go under the 3rd parameter of the function FormControlDirective
, which is: valueAccessors: ControlValueAccessor[]
Can anyone offer insights on how to properly mock this scenario?