backbone.js click event spy is not getting called using jasmine.js and sinon.js

前端 未结 3 1037
无人及你
无人及你 2020-12-28 16:34

I am trying to test a button click using backbone.js, jasmine.js and sinon.js. But the following test case fails. I am using a spy to track whether it is getting called or n

3条回答
  •  难免孤独
    2020-12-28 17:04

    The problem is you add your spy after backbone has already bound the click event directly to the addTask function (it does that during construction of the View). Therefore your spy will not get called.

    Try attaching the spy to a prototype of the View before you construct it. Like this:

    this.addTaskSpy = sinon.spy(T.views.NewViewTask.prototype, 'addTaskSpy');
    this.view = new T.views.NewTaskView();
    

    and then remember to remove it:

    T.views.NewViewTask.prototype.addTaskSpy.restore()
    

提交回复
热议问题