You must make sure, window.open() returns a fully featured object since the printContent method under test uses properties and functions of windowPrint. Such an object is typically created with createSpyObj.var doc = jasmine.createSpyObj('document', ['write', 'close']);var windowPrint = jasmine.createSpyObj('windowPrint', ['focus', 'print', 'close']);windowPrint.document = doc;spyOn(window, 'open').and.returnValue(windowPrint);您修改后的单元测试将如下所示:Your amended unit test would then look as follows: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();}); 这篇关于如何为打印机功能编写Jasmine测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 06:03