angular - 如何从单元测试访问注入(inject)的 ngControl?

标签 angular unit-testing

如何从单元测试中访问注入(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/

相关文章:

typescript - 如何在 TypeScript 中扩展一个类?

javascript - 在 Angular 中使用源映射生成生产版本 - CLI

javascript - 如何使用 ElementRef 关闭外部点击 Angular 4 上的下拉菜单

html - Angular2 如何在 HTML5 模板中显示 localStorage 值?

C#:从测试方法调用时方法失败,但在其他情况下工作正常

node.js - 未捕获( promise ): Error: No provider for GooglePlus

php - 如何从命令行运行使用 Google App Engine PHP SDK 函数的应用测试

java - 模拟在java中被多次调用的方法

node.js - 如何使用从 Controller 返回的值?在 NestJs 上测试 Controller

c# - 为 HttpContext.Current.Session 编写单元测试时出现问题