【问题标题】:How do you access an attribute directive within an Angular component for specs?您如何访问 Angular 组件中的属性指令以获取规范?
【发布时间】:2018-11-23 13:28:21
【问题描述】:

我正在为使用 ngx-bootstrap 的工具提示属性指令 (TooltipDirective) 的组件编写规范。我想调用tooltip的open方法来查看内容。但是当我尝试访问该指令时,我得到了我的组件。

const tooltip = fixture.debugElement.query(By.directive(TooltipDirective))
                       .componentInstance;

当我console.log(tooltip) 时,我得到了对被测组件的引用,而不是预期的工具提示指令。有什么方法可以访问属性指令吗?

【问题讨论】:

    标签: angular jasmine


    【解决方案1】:

    为了在组件上获得属性指令以进行测试,您必须在构建它的DebugElement 上使用injector。操作方法如下:

    const tooltip = debugElement.query(By.directive(TooltipDirective))
                                .injector.get(TooltipDirective);
    

    您不能使用 TestBed.get 访问此指令,因为它仅在子注入器上可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2023-04-06
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2014-01-20
      • 2020-05-30
      相关资源
      最近更新 更多