angular - 需要帮助编写打印功能的茉莉花单元测试用例
问题描述
我已经为这种情况搜索了解决方案。我什么也没找到。所以,我作为一个新问题发布。我在组件中有一个方法,可以在文档中打印数据,例如 (control+P)。
printContents;
popupWin;
print(): void {
this.printContents = document.getElementById('print-section2').innerHTML;
this.popupWin = window.open('', '_blank', 'top=500,left=500,height=100%,width=auto');
this.popupWin.document.open();
this.popupWin.document.write(`
<html>
<head>
<title>Zone 24x7 - Resource Allocation System
</title>
</br>
</br>
<style>table,tr,td{
border-collapse:collapse;
border:0.5px solid #000000;
}</style>
<style>h3{justify-content:center}
td{
width: 40%
}
</style>
</head>
<body onload="window.print();window.close()">${this.printContents}</body>
</html>`
);
this.popupWin.print();
this.popupWin.document.close();
}
我试图为那个写下面的测试用例。
it('Print method should called', () => {
let viewEditResourceService = fixture.debugElement.injector.get(ViewEditResourceService);
spyOn(viewEditResourceService, 'getResourcebyId').and.returnValue(Observable.of(allocationModalCompObj.resDetails.data));
component.message = 298;
spyOn(window, 'open');
spyOn(document, 'open');
spyOn(document, 'write');
spyOn(window, 'print');
component.ngOnInit();
component.print();
expect(window.open).toHaveBeenCalled();
expect(window.print).toHaveBeenCalled();
});
它给出以下错误
TypeError:无法读取未定义的属性“文档” TypeError:无法读取未定义的属性“文档”
我明白这个错误是因为间谍窗口对象。非常感谢任何帮助。
解决方案
推荐阅读
- python-3.x - 关于python中的statsmodels的导入错误
- python - MySQL 准备好的语句导致 SQL 语法错误
- java - 如何动态更改回收站视图项?
- objective-c++ - : 错误: 预期的成员名称或';' 在声明说明符之后;'friend' 是 Objective-C++ 中的关键字 @property(
- google-apps-script - 有空单元格时如何使用脚本插入公式
- javascript - 如何禁用 eslint nextline 的多个规则
- intellij-idea - Ktor 应用程序未在使用 IntelliJ IDEA 的 Kotlin 多平台项目中运行
- r - 重新创建 diag() 函数
- c++ - LibPng - 使用不同的设置两次写入图像
- c++ - 为什么包含 iostream 时此代码更快?