Backbone Routerで特定のルート上で適切なメソッドを呼び出していることを確認するためにメソッド呼び出しを呼び出すことに問題がある。Backbone.jsでスパイするジャスミンで電話をかける
抜粋
class App.Router extends Backbone.Router
routes:
'' : 'index'
index: ->
console.log "router.index has been called"
ルータ
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.indexが呼び出された」オリジナルRouter.index関数からテスト出力に
感謝をログ出力を見ることができる他の変異体
it "should call index", ->
spyOn(@router, "index")
@router.navigate('', true)
expect(@router.index).toHaveBeenCalled()
を試してみました!
EDIT: 一つの解決策
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
これはルートのトリガーをテストする実行可能な方法ではありません。これは、インデックス関数がコールバックとしてルーターにバインドされたものであり、ルートがトリガーされたときに関数が実際に呼び出されるということではありません。 「[this S.O.ルータがURLと一致したときに対応するルート関数が呼び出されたことを確認する方法については、[post](http://stackoverflow.com/questions/9215737/testing-routers-in-backbone-js-properly)を参照してください。 – louism2