【发布时间】:2020-05-29 17:41:12
【问题描述】:
我必须测试这种奇怪的情况。在我的 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);
});
}));
但两者都返回上述错误。我怎么能解决这个问题?
【问题讨论】:
标签: angular unit-testing karma-jasmine angular-test