我最近在我的一个
javascript函数中添加了一个e.preventDefault(),它破坏了我的jasmine规范.我试过spyOn(e,’preventDefault’).和返回(true);但我得到e是未定义的错误.我如何存根e.preventDefault()?
showTopic: function(e) { e.preventDefault(); midParent.prototype.showTopic.call(this,this.model,popup); this.topic.render(); } it("calls the parent",function() { var parentSpy = spyOn(midParent.prototype,"showTopic"); this.view.topic = { render: function() {} }; this.view.showTopic(); expect(parentSpy).toHaveBeenCalled(); });
解决方法
@H_502_7@ 创建模拟对象(使用您需要的间谍)的另一种方法是使用jasmine.createSpyObj().包含间谍名称的数组必须作为第二个参数传递.
var e = jasmine.createSpyObj('e',[ 'preventDefault' ]); this.view.showTopic(e); expect(e.preventDefault).toHaveBeenCalled();