testing - DebugElement.query 不适用于在规范中动态添加到 dom 的元素
问题描述
我有一个应用程序使用 ngx-bootstrap 在鼠标悬停时显示工具提示。我想测试动态添加的内容是否正确显示。为了做到这一点,我有一个看起来像这样的测试:
it(shows the right tooltip', fakeAsync(() => {
fixture.debugElement.query(By.directive(TooltipDirective))
.triggerEventHandler('mouseover', null);
tick();
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.tooltip-inner')).nativeElement)
.toBe('the tooltip text');
}
这会导致错误指示fixture.debugElement.query(By.css('.tooltip-inner'))
:“无法读取 null 的属性‘nativeElement’”
如果我打印出fixture.debugElement.nativeElement
我得到这个的内容:
<div id="root1" ng-version="5.2.9">
<my-component>
<div ng-reflect-tooltip="the tooltip text">
<img src="images/test.png">
</div>
<bs-tooltip-container role="tooltip" class="tooltip in tooltip-right">
<div class="tooltip-arrow arrow"></div>
<div class="tooltip-inner">the tooltip text</div>
</bs-tooltip-container>
<my-component>
</div>
重要的一点是 html 存在 - 它只是无法通过DebugElement.query
.
我目前获得规范通过的解决方案是将期望更改为:
expect(fixture.debugElement.nativeElement.textContent.trim())
.toBe('the tooltip text');
这是可行的,但如果我遇到具有多个工具提示的类似情况(例如),它就会崩溃。有没有人能够以更好的方式处理这个问题?我没有正确设置这个规范吗?
解决方案
推荐阅读
- sql - SQL Server 2008:复制一行 n 次,其中 n 是字段中的值
- javascript - 在静态方法递归使用的 es6 类中创建计数器变量
- facebook - Facebook Webhooks 提及事件 json 不包含发件人 fb 用户 ID 和用户名
- jquery - 如何解决此问题:跨域读取阻塞 (CORB) 阻止了跨域响应
- django - Django Rest框架序列化器有必要吗?
- maven - 如何在 Sonatype nexus oss 3 中设置虚拟存储库
- scala - 为什么 Phantom Cassandra Insert 得到“main” java.lang.NoClassDefFoundError: scala/reflect/runtime/package 错误
- python - 字典计数时间戳错误
- asp.net-core - 是否可以从 Asp.Net Core Identity 公开 Security Stamp 作为 OIDC 的声明
- c# - 版本中更改了 Protobuf-net 枚举序列化行为。2.3.0