最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stubbing e.preventDefault() in a jasmine test - Stack Overflow

programmeradmin1浏览0评论

I recently added an e.preventDefault() to one of my javascript functions and it broke my jasmine spec. I've tried spyOn(e, 'preventDefault').andReturn(true); but I get e is undefined error. How do I 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();
});

I recently added an e.preventDefault() to one of my javascript functions and it broke my jasmine spec. I've tried spyOn(e, 'preventDefault').andReturn(true); but I get e is undefined error. How do I 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();
});
Share Improve this question edited Apr 11, 2013 at 5:22 Huy asked Apr 11, 2013 at 5:11 HuyHuy 11.2k14 gold badges59 silver badges102 bronze badges 3
  • Did the solution not work? – user1726343 Commented Apr 11, 2013 at 5:27
  • @Asad no, that was why I deleted it – Huy Commented Apr 11, 2013 at 5:43
  • Your spyOn(e, 'preventDefault') is ok but you didn't pass e to the showTopic() in the it(). Is that the point? – zbynour Commented Apr 11, 2013 at 6:49
Add a comment  | 

4 Answers 4

Reset to default 28

Another way to create mock object (with spies you need) is to use jasmine.createSpyObj(). Array containing spy names have to be passed as second parameter.

var e = jasmine.createSpyObj('e', [ 'preventDefault' ]);
this.view.showTopic(e);
expect(e.preventDefault).toHaveBeenCalled();

don't know this approach is correct or not but works for me, feel free to tell if it is not a good approach

step1:create a eventStub with the function for prevent default

 const eventStub = {
      preventDeafult() {}
    }

step2: write the 'it' Block:

it('should preventDefault when dragover event occurs', () => {
  // step 3 goes here
  // step 4 goes here
  // step 5 goes here
});

step3:create spy for prevent default:

const spy = spyOn(eventStub, 'preventDefault');

step4:trigger the event let's say dragover and pass the eventStub

  component.triggerEventHandler('dragover', eventStub)

step5:write the assertion

expect(spy).tohaveBeenCalled()

Note: "component" in step 4 is the instance of component we get from fixture Example to get component instance from fixture:

let fixture = TestBed.createComponent(<your Component's Class Name>);
let component = fixture.componentInstance;

try and let me know if it works for you thanks you,,,, Happyyy coding :-) !!!!!

You have to pass an object with a field preventDefault that holds your spy:

var event = {preventDefault: jasmine.createSpy()}
this.view.showTopic(event);
expect(event.preventDefault).toHaveBeenCalled

This is very similar to approaches on top. I just mocked out the event and passed preventDefault with an sinon spy. The difference was that I had to identify the type which was a click on my test.

var e = {
    type: 'click',
    preventDefault: sinon.spy()
};
发布评论

评论列表(0)

  1. 暂无评论