如何从单元测试中访问注入(inject)的ngControl?
如何解决错误?

在组件中

constructor(
    @Self() @Optional() public ngControl: NgControl
  ) { }

ngOnInit(): void {
    this.ngControl.valueChanges

在单元测试中
beforeEach(() => {
      fixture = TestBed.createComponent(component);
      fixture.detectChanges();

执行测试时出现错误TypeError: Cannot read property 'valueChanges' of null

最佳答案

适用于:

beforeEach(() => {
     fixture = TestBed.createComponent(сomponent);
     (fixture.componentInstance as any).ngControl = new FormControl();

关于angular - 如何从单元测试中访问注入(inject)的ngControl?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51618154/

10-10 16:45