1

次のバックボーンルーターがあるとします。

class App.Routers.ThingsRouter extends Backbone.Router
  initialize: -> new App.Collections.ThingsCollection()

  index: ->
    that = this
    @collection.fetch success: ->
      view = new App.Views.ThingsIndex(collection: that.collection)
      $('#app-container').html(view.render().el)

私はこれを監視し、それApp.Views.ThingsIndex()が呼び出されることを確認できるジャスミンスパイを書く必要があります。ただし、AJAXであるため、以下は機能しません。

describe 'index', ->
  @router = new App.Routers.ThingsRouter()
  spyOn(@router.collection, 'fetch')
  fake = { render: -> '' }
  @previewsIndexStub = spyOn(Periscope.Views, 'PreviewsIndex').andReturn(fake)
  @router.index()
  expect(@previewsIndexStub).toHaveBeenCalled()

Jasmineは、AJAX呼び出しが完了する前に期待値関数を実行するためです。このようなコールバックをテストする良い方法はありますか?

4

1 に答える 1