title: Angular 8 Unit Test, Cannot set property 'valueAccessor' of null
body: <p>I'm trying to simply <strong>pass the test</strong> <em>(should create - component=> toBeTruthy)</em> for the custom component (angular component class) implemented with <strong>ControlValueAccessor</strong>. I've injected NgControl in <code>constructor</code> and pass the <code>this</code> keyword.</p>
  
  <pre><code> constructor(@Optional() @Self() public controlDir?: NgControl) {
      this.controlDir.valueAccessor = this;
    } 
  </code></pre>
  
  <p>And  then on ngOnInit()</p>
  
  <pre><code>ngOnInit() {
      const control = this.controlDir.control;
      const validators = control.validator ? [control.validator] : [];
      const asyncValidators = control.asyncValidator ? [control.asyncValidator] : [];
  
      control.setValidators(validators);
      control.setAsyncValidators(asyncValidators);
      control.updateValueAndValidity();
    }
  </code></pre>
  
  <p>In the test file </p>
  
  <pre><code>describe('TextInputComponent', () =&gt; {
    let component: TextInputComponent;
    let fixture: ComponentFixture&lt;TextInputComponent&gt;;
  
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        declarations: [TextInputComponent],
        imports: [ReactiveFormsModule]
      })
        .compileComponents();
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(TextInputComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
  
    it('should create', () =&gt; {
      expect(component).toBeTruthy();
    });
  
  });
  </code></pre>
  
  <p>And the errors</p>
  
  <blockquote>
    <p>TypeError: Cannot set property 'valueAccessor' of null</p>
  </blockquote>
  
  <p>I'd be appreciated if anyone shares your ideas to solve this issue? </p>
  
  <p>Thank you.
  Rabin</p>