Szpiegowanie wywołań trasy Backbone.js za pomocą Jasmine
Mając problemy ze szpiegowaniem metody wywołuje router Backbone, aby upewnić się, że wywołuje właściwą metodę na danej trasie.
fragment testu
describe 'Router', ->
beforeEach ->
@router = new App.Router()
Backbone.history.start()
afterEach ->
Backbone.history.stop()
describe 'routes', ->
it 'should be defined', ->
expect(@router.routes).toBeDefined()
describe 'default route', ->
it 'should be defined', ->
expect(@router.routes['']).toBeDefined()
it 'should call index', ->
spy = spyOn(@router, "index")
@router.navigate('', true)
expect(spy).toHaveBeenCalled()
Router
class App.Router extends Backbone.Router
routes:
'' : 'index'
index: ->
console.log "router.index has been called"
Wszystko mija, z wyjątkiem ostatniego testu „powinien wywoływać indeks”. Niepowodzenie z komunikatem „Oczekiwano wywołania indeksu szpiegowskiego”. Próbowałem innych wariantów
it "should call index", ->
spyOn(@router, "index")
@router.navigate('', true)
expect(@router.index).toHaveBeenCalled()
Widzę również wyjście dziennika „router.index” w wynikach testu z oryginalnej funkcji Router.index
Dzięki!
EDYCJA: Jedno rozwiązanie
describe '#1 Solution', ->
it 'should call index', ->
spyOn(App.Router.prototype, "index")
@router = new App.Router()
Backbone.history.start()
@router.navigate('', true)
expect(App.Router.prototype.index).toHaveBeenCalled()