jasmine - 如何使用茉莉花测试翻译服务中的字段标签
问题描述
我是使用 jasmine 对角度代码进行单元测试的新手。我正在尝试测试从 HTML 文件中的翻译服务获得的字段标签。我使用了以下代码,但出现错误“失败:无法读取 null 的属性‘textContent’”
it('label should be Enter your mobile number', async(() => {
const de = fixture.debugElement;
expect(fixture.debugElement.nativeElement.querySelector('[id=textFormat] label').textContent).toEqual('ENTER_YOUR_MOBILE_NUMBER');
translate.setTranslation('en', { ENTER_YOUR_MOBILE_NUMBER : 'Enter your mobile number' });
translate.use('en');
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.querySelector('[id=textFormat] label').textContent).toEqual('Enter your mobile number');
}));
HTML :
<div class="form-group" id="email">
<label for="mobileNumber" class="col-form-label col-form-label-lg" id="textFormat">
{{ 'ENTER_YOUR_MOBILE_NUMBER' | translate }} <span class="labelHint" id="numbFormat">(05XXXXXXXX)</span>
</label>
</div>
解决方案
我认为你的 CSS 选择器不准确,试试这个:
it('label should be Enter your mobile number', async(() => {
const de = fixture.debugElement;
expect(fixture.debugElement.nativeElement.querySelector('label#textFormat').textContent).toEqual('ENTER_YOUR_MOBILE_NUMBER');
translate.setTranslation('en', { ENTER_YOUR_MOBILE_NUMBER : 'Enter your mobile number' });
translate.use('en');
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.querySelector('label#textForma').textContent).toEqual('Enter your mobile number');
}));
更改querySelector
.
推荐阅读
- android-studio - 在 linarLayout 的中心创建 textViews
- javascript - 是否可以通过 React 编写的站点中的元素获取事件处理程序?
- android - 在垂直 RecyclerView 内的水平 RecyclerView 上垂直滚动时,AppBarLayout 不会折叠/展开
- sql - 基于 10 天过滤器生成 SQL 序列号(Row_Numbers)
- ios - AppStoreConnect 中缺少“上传应用程序图标”选项
- python - Python Argparse:仅返回没有参数名称的值
- postgresql - 基于日期的分区对于表的主键有问题 - 任何解决方法?
- python-3.x - 将数据帧的每一行转换为元组列表,列名和值作为元组
- python - 烧瓶代码参数中的forloop不循环第二个位置
- python - 为什么程序只运行一次