在Backbone Router上监视方法调用时遇到问题,以确保它在给定路由上调用正确的方法.
从测试中摘录
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()
Run Code Online (Sandbox Code Playgroud)
路由器
class App.Router extends Backbone.Router
routes:
'' : 'index'
index: ->
console.log "router.index has been called"
Run Code Online (Sandbox Code Playgroud)
除了上一次测试"应该调用索引"之外,一切都通过了.它失败并显示消息"已调用预期的间谍索引".我尝试了其他变种
it "should call index", ->
spyOn(@router, "index")
@router.navigate('', true)
expect(@router.index).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)
我还可以在原始Router.index函数的测试输出中看到"router.index已被调用"日志输出
谢谢!
编辑:一个解决方案
describe '#1 Solution', …Run Code Online (Sandbox Code Playgroud)