How-to test starting Backbone's history
You can call Backbone.history.start()
once. Multiple calls
causes Backbone to throw an Error saying: “Backbone.history has already been started”.
You’ll hit this error when testing, if you have each test case set up your application, and your application’s setup includes starting Backbone:
App =
initialize: (data) ->
# ...
if not Backbone.history.started?
Backbone.history.start({pushState: true})
Backbone.history.started = true
You solve this by calling Backbone.history.stop()
prior to initializing your application:
describe "App", ->
describe "#initialize", ->
it "starts Backbone.history", ->
Backbone.history.started = null
Backbone.history.stop()
sinon.spy(Backbone.history, "start")
App.initialize {}
expect(Backbone.history.start).toHaveBeenCalled()
Backbone.history.start.restore()