angular - 当在 html 中进行方法调用时,使用 jasmine 对组件进行角度测试
问题描述
我必须测试这种奇怪的情况。在我的 html 中,我有:
<div id="date">
<h5>
{{ showFormattedDate(myDate) }}
</h5>
</div>
函数showFormattedDate()
定义.ts
如下:
showFormattedDate(dateToFormat: string): string {
return moment(dateToFormat).format('HH:mm DD/M/YYYY');
}
现在我正在尝试对此进行测试,但我不断收到此错误:
预计“在无效日期创建”为“12:23 29/5/2020”。
无效的日期部分也是我在页面加载时在屏幕上看到的一秒钟,但我认为这是因为函数调用。
我尝试使用以下两种方式对其进行测试:
it('should display the date correctly', async () => {
fixture.detectChanges();
// await fixture.whenStable();
// await fixture.isStable();
// await fixture.whenRenderingDone();
expect(el.query(By.css('#date')).nativeElement.textContent).toContain(
getTestData().date);
});
it('should display the date correctly (async)', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
// wait for async getQuote
fixture.detectChanges(); // update view with quote
expect(el.query(By.css('#date')).nativeElement.textContent).toContain(getTestData().date);
});
}));
但两者都返回上述错误。我怎么能解决这个问题?
解决方案
推荐阅读
- android - 您如何为付费的亚马逊应用程序实施许可?
- c++ - 可以在函数内部执行 new 并返回指向它的指针吗?
- python - 如何在 pandas 中使用 if 语句来舍入一列
- ios - 设置 textfield.textContentType = .telephoneNumber 并按下建议的电话号码后没有任何反应
- html - 媒体查询没有像我预期的那样工作,我在这里做错了吗?
- r - ggplot2 中 pdf 导出的一致绘图大小
- kubernetes - 如何在同一个K8S集群上安装多个redis?
- ansible - Ansible:如何从列表中检查远程文件夹中是否存在多个文件
- sql - 在列中包含今天日期的 VBA 查询数据
- node.js - Firebase 管理员使用 BCRYPT 密码导入用户不起作用