如何从单元测试中访问注入(inject)的 ngControl? 如何解决错误?
在组件中
constructor(
@Self() @Optional() public ngControl: NgControl
) { }
ngOnInit(): void {
this.ngControl.valueChanges
在单元测试中
beforeEach(() => {
fixture = TestBed.createComponent(component);
fixture.detectChanges();
执行测试时报错
类型错误:无法读取 null 的属性“valueChanges”
最佳答案
适用于:
beforeEach(() => {
fixture = TestBed.createComponent(сomponent);
(fixture.componentInstance as any).ngControl = new FormControl();
关于angular - 如何从单元测试访问注入(inject)的 ngControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51618154/