के साथ कॉल कॉलिंग समस्याएं होने के कारण बैकबोन राउटर पर विधि कॉल करने की समस्याएं यह सुनिश्चित करने के लिए कि किसी दिए गए मार्ग पर सही विधि कहें। परीक्षणबैकबोन.जेएस रूट पर जासूसी जैस्मीन
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()
रूटर
class App.Router extends Backbone.Router
routes:
'' : 'index'
index: ->
console.log "router.index has been called"
सब कुछ अंतिम परीक्षण "सूचकांक बुलाना चाहिए" को छोड़कर गुजरता से
अंश। यह संदेश के साथ विफल रहता है "अपेक्षित जासूसी सूचकांक कहा जाता है"। Ive अन्य वेरिएंट
it "should call index", ->
spyOn(@router, "index")
@router.navigate('', true)
expect(@router.index).toHaveBeenCalled()
की कोशिश की मैं भी देख सकते हैं मूल Router.index समारोह से परीक्षण उत्पादन में लॉग उत्पादन
धन्यवाद "router.index बुलाया गया है"!
संपादित करें: एक समाधान
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()
धन्यवाद! ऐसा लगता है कि जाने का रास्ता है :) – eldewall
यह मार्ग के ट्रिगरिंग का परीक्षण करने का एक व्यवहार्य तरीका नहीं है। यह सब परीक्षण है कि इंडेक्स फ़ंक्शन राउटर से कॉलबैक के रूप में बाध्य था, न कि जब मार्ग को ट्रिगर किया गया था तो वास्तव में फ़ंक्शन को बुलाया जाता है। देखें [यह एसओ पोस्ट] (http://stackoverflow.com/questions/9215737/testing-routers-in-backbone-js-properly) यह सत्यापित करने के लिए कि राउटर यूआरएल से मेल खाता है तो संबंधित रूट फ़ंक्शन को कॉल किया गया था। – louism2