首页 > 解决方案 > 需要帮助编写打印功能的茉莉花单元测试用例

问题描述

我已经为这种情况搜索了解决方案。我什么也没找到。所以,我作为一个新问题发布。我在组件中有一个方法,可以在文档中打印数据,例如 (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:无法读取未定义的属性“文档”

我明白这个错误是因为间谍窗口对象。非常感谢任何帮助。

标签: angularjasminewindowspy

解决方案


推荐阅读