我最近在我的一个 javascript 函数中添加了一个 e.preventDefault()
,它破坏了我的 jasmine 规范。我试过 spyOn(e, 'preventDefault').andReturn(true);
但我得到 e
is undefined 错误。如何 stub 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();
});
最佳答案
另一种创建模拟对象(带有你需要的 spy )的方法是使用 jasmine.createSpyObj()
。
包含 spy 名称的数组必须作为第二个参数传递。
var e = jasmine.createSpyObj('e', [ 'preventDefault' ]);
this.view.showTopic(e);
expect(e.preventDefault).toHaveBeenCalled();
关于javascript - 在 Jasmine 测试中 stub e.preventDefault(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941181/