如何从单元测试访问注入的 ngControl? [英] How make access to injected ngControl from unit tests?

查看:28
本文介绍了如何从单元测试访问注入的 ngControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How make access to injected ngControl from unit tests? How resolve the error?

在组件中

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();

这篇关于如何从单元测试访问注入的 ngControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆