javascript - 在 Jasmine 测试中 stub e.preventDefault()

标签 javascript jasmine

我最近在我的一个 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/

相关文章:

java - Vaadin 7.1.1 : Failed to load the widgetset

javascript - 工具提示不会在鼠标移出时消失

angularjs - 在 Jasmine 测试中获取自定义指令的 Controller 范围

javascript - 无法读取 JavaScript 函数的属性

用于验证 12 小时时间的 JavaScript 正则表达式

javascript - 从串行端口 -> Chrome 应用程序 -> 网页进行通信。网页启动。

javascript - Angular 2 : How to unit test tabs component with tightly coupled parent and child components

angularjs - Protractor+AngularJS+Jasmine - 测试按住项目

javascript - 如何使用jasmine测试一个需要很长时间才能响应的异步函数?

javascript - 如何对 Angular 1.5 组件中的函数进行单元测试