Spying on Backbone.js route calls with Jasmine

前端 未结 2 1682
暗喜
暗喜 2021-02-06 04:13

Having problems spying method calls on a Backbone Router to ensure it calles the right method on a given route.

excerpt from the test

describe \'Router\'         


        
2条回答
  •  梦毁少年i
    2021-02-06 04:51

    I'm pretty sure this has to do with the way that Backbone binds to its routing methods when you use a routes hash (especially if you're seeing a console log correctly output). That is, the router has bound to the original index method, but your spy has replaced the "current" index method.

    You have two options:

    • spyOn(@router, "index") before you the router binds to the routes (may be difficult)
    • Spy on the prototype's index method: spyOn(App.router.prototype, "index"); @router.navigate('', true); expect(App.router.prototype.index).toHaveBeenCalled();

提交回复
热议问题