Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编写打印机功能的Jasmine测试_Javascript_Angular_Typescript_Jasmine - Fatal编程技术网

Javascript 如何编写打印机功能的Jasmine测试

Javascript 如何编写打印机功能的Jasmine测试,javascript,angular,typescript,jasmine,Javascript,Angular,Typescript,Jasmine,我正在尝试为以下打印功能编写Jasmine测试: printContent( contentName: string ) { this._console.Information( `${this.codeName}.printContent: ${contentName}`) let printContents = document.getElementById( contentName ).innerHTML; const windowPrint = window.

我正在尝试为以下打印功能编写Jasmine测试:

  printContent( contentName: string ) {
    this._console.Information( `${this.codeName}.printContent: ${contentName}`)
    let printContents = document.getElementById( contentName ).innerHTML;
    const windowPrint = window.open('', '', 'left=0,top=0,width=925,height=1400,toolbar=0,scrollbars=0,status=0');
    windowPrint.document.write(printContents);
    windowPrint.document.close();
    windowPrint.focus();
    windowPrint.print();
    windowPrint.close();
  }
我非常愿意将函数更改为更易于测试。这是我目前的测试:

  it( 'printContent should open a window ...', fakeAsync( () => {
    spyOn( window, 'open' );
    sut.printContent( 'printContent' );
    expect( window.open ).toHaveBeenCalled();
  }) );

我正在尝试获得更好的代码覆盖率。

您必须确保,
window.open()
返回一个功能齐全的对象,因为测试中的
printContent
方法使用
windowPrint
的属性和函数。这样的对象通常是使用创建的

修改后的单元测试将如下所示:

it( 'printContent should open a window ...', () => {

  // given
  var doc = jasmine.createSpyObj('document', ['write', 'close']);
  var windowPrint = jasmine.createSpyObj('windowPrint', ['focus', 'print', 'close']);
  windowPrint.document = doc;
  spyOn(window, 'open').and.returnValue(windowPrint);

  // when
  sut.printContent('printContent');

  // then
  expect(window.open).toHaveBeenCalledWith('', '', 'left=0,top=0,width=925,height=1400,toolbar=0,scrollbars=0,status=0');
  expect(doc.write).toHaveBeenCalled(); 
  expect(doc.close).toHaveBeenCalled(); 
  expect(windowPrint.focus).toHaveBeenCalled(); 
  expect(windowPrint.print).toHaveBeenCalled(); 
  expect(windowPrint.close).toHaveBeenCalled(); 
});

你目前的考试有什么问题?
it( 'printContent should open a window ...', () => {

  // given
  var doc = jasmine.createSpyObj('document', ['write', 'close']);
  var windowPrint = jasmine.createSpyObj('windowPrint', ['focus', 'print', 'close']);
  windowPrint.document = doc;
  spyOn(window, 'open').and.returnValue(windowPrint);

  // when
  sut.printContent('printContent');

  // then
  expect(window.open).toHaveBeenCalledWith('', '', 'left=0,top=0,width=925,height=1400,toolbar=0,scrollbars=0,status=0');
  expect(doc.write).toHaveBeenCalled(); 
  expect(doc.close).toHaveBeenCalled(); 
  expect(windowPrint.focus).toHaveBeenCalled(); 
  expect(windowPrint.print).toHaveBeenCalled(); 
  expect(windowPrint.close).toHaveBeenCalled(); 
});