So I’ve just started to write tests for my in-progress javascript app, using sinon.js & jasmine.js. Works pretty well overall, but I need to also be able to test my routers.
The routers, in their current state, will trigger an number of views and other stuff, terminating the current jasmine.js test by invoking Backbone.navigate dependent on application state and UI itneraction.
So how could I test that routing to different locations would work, while keeping the routers “sandboxed” and not allowing them to change route?
Can I set up some sort of mock function that will monitor pushState changes or similar?
Here’s what I ended up using myself. I made a mock version of the router by extending it and overriding the methods with a blank method to prevent it from invoking any further logic when being called:
Note that
sinon.jsis used above to create the spy, along withunderscore.jsto provide thesizefunction.