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\'
It has took too much time to me to come with a working jsFiddle and the question has been already answered by @MarkRushakoff.
Still I have some comments.
The way Backbone is binding the routes make very difficult to test it.
The point is that the router methods are not called directly in the router instance, the methods are taked as callbacks and stored in an internal Backbone.history.route
waiting for execution, check the Backbone.Router.route code.
This operation is done in the moment the Router
is instantiate, so you have to spy
your Router.method before you instantiate the reference, so for you have to delay Backbone.history.start
also after the spy
has been activated.
As you have to declare the spy
before the router instance is created you have to do it in a Class level.
Said so this is the simplest solution I came with:
describe("Router", function() {
afterEach( function(){
Backbone.history.stop();
});
it("should call index", function(){
spyOn(App.Router.prototype, "index")
var router = new App.Router(); // instance created after spy activation
Backbone.history.start(); // it has to start after the Router instance is created
router.navigate('', true);
expect(App.Router.prototype.index).toHaveBeenCalled();
});
});
Conclusion, I think the Backbone.Router
implementation has not an intuitive design.