2015-11-07 102 views
2

我想写一些单元测试,但他们失败。你能建议我正确的做法吗?茉莉花单元测试window.onbeforeunload

show(contactId: string, phoneNumber: string, contactType: string, section: string, memberId: string) { 
 
     this.$window.onbeforeunload =() => ""; 
 
     $('.disabledcalldialog').on("click", e => { 
 
      this.$window.alert('Please close call dialog and try.'); 
 
      e.preventDefault(); 
 
     });

我的规格文件就像

beforeEach(() => { 
 
     inject(($rootScope: ng.IScope, $window) => { 
 
    spyOn($window, 'onbeforeunload'); 
 
      $(window).trigger('onbeforeunload'); 
 
    }); 
 
    it('should be able to call when changing URL',() => { 
 
     
 
     expect($window.onbeforeunload).toHaveBeenCalled();

噶抛出的错误信息,如“类型错误:未定义或空无法获取财产 'onbeforeunload'参考”;

回答

0

你试过:

$window.trigger('onbeforeunload'); 

代替:

$(window).trigger('onbeforeunload');