angular - 编写 Jasmine 测试以检查 h4 元素的内容
问题描述
我有一个测试,我基本上是在检查 h4 标签。现在在该标签中,我希望能够检查其中给出的内容。我似乎正在编写以下测试,但它一直失败。我不确定我错过了什么或者我的测试写错了:
describe('StatusAppComponent', () => {
it('should consist of a heading four tag', () => {
component.showBanner = true;
fixture.detectChanges();
const headingElems = fixture.debugElement.query(By.css('h4'));
expect(headingElems).toHaveText('WARNING')
});
我的html如下:
<div *ngIf="this.showBanner">
<div class="banner alert">
<div class="page-section">
<h4>WARNING</h4>
{{this.message}}
</div>
解决方案
这并不总是必要的,但有时您必须等到视图稳定后才能查询 HTML 元素。
it('should consist of a heading four tag', () => {
component.showBanner = true;
fixture.detectChanges();
fixture.whenStable().then(() => {
const headingElems = fixture.nativeElement.querySelector('h4');
expect((headingElems.textContent as string).trim()).toBe('WARNING');
});
});
推荐阅读
- java - 我正在创建的侦听器接口中不允许自定义适配器?
- javascript - 反应钩子不保存数据
- android - 出错时继续执行链式 RxJava observable,仅在满足特定条件时停止执行
- ssl - openssl DTLS 1.0.2k 是否接受分段输入并重新组装分段数据包?
- ruby-on-rails - 测试改变计数器的 Rspec after_action
- html - 如何处理“div”的垂直对齐?
- r - 如何使用不等的样本量 R 运行 ANOVA/多因素回归
- javascript - 使用 Jimp Composite 重叠两个图像
- flutter - 如何禁用 PageView 边缘的动画?
- python - Popen 在错误的时间执行