我需要帮助来编写方法的单元测试,但我不知道如何完成。所以,如果有人能帮我一把,你就会拯救我的一天。
我的方法:
print(component) {
window.print();
window.cancel();
}
我该如何测试这种方法?
试试这样的东西:
it('should call print and cancel', () => {
// spy on print and cancel on window object
const printSpy = spyOn(window, 'print');
const cancelSpy = spyOn(window, 'cancel');
// call print
component.print({/* mock component however you wish here */});
// expect print and cancel spy to have been called
expect(printSpy).toHaveBeenCalled();
expect(cancelSpy).toHaveBeenCalled();
});
如果你是单元测试的新手,这是一个很好的资源:https://testing-angular.com/.