【发布时间】:2012-04-19 13:27:06
【问题描述】:
假设我有以下主干路由器:
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)
我需要编写一个 Jasmine 间谍来监视这个并确保调用 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 调用完成之前运行了期望函数。有没有像这样测试回调的好方法?
【问题讨论】:
标签: ruby-on-rails backbone.js coffeescript jasmine