0

我试图弄清楚如何使用示例数据测试 Backbone.Collection,但每次我调用fetchCollection 时,它的行为似乎都不像我期望的那样。例如,使用 jasmine 和 coffeescript,

describe "task model", ->
  testData = [
      { id: 1, type: "personal", complete: false }
      { id: 2, type: "business", complete: true }
  ]

  collection = null
  item = null
  testTasks = Alloy.createCollection "task"

   addTask = (t) ->
     #newTask = new model t
     Ti.API.info t
     testTasks.add new Alloy.createModel "task", t
     # testTasks.length is 2 which is correct
     Ti.API.info "testTasks.length after add is #{testTasks.length}"

   # add test data to a collection to use for tests/dev
   addTask t for t in testData

   beforeEach ->
     collection = Alloy.createCollection "task", testTasks
     item = Alloy.createModel "task"
     collection.fetch view

   # fails: Expected 0 to be 2
   it "has sample data for development", ->
     collection.fetch view
     expect(collection.length).toEqual 2

我正在使用一个名为 Titanium Alloy 的框架,它使用 BackboneJS 0.9.2

4

1 回答 1

1

您需要等待您的断言,直到获取集合。

$.when( collection.fetch view).then (data, textStatus, jqXHR )->
  expect(collection.length).toEqual 2

或者

collection.fetch view,
  checkFunction = ->
    expect(collection.length).toEqual 2
  success: =>
    checkFunction()
  error: =>
    checkFunction()
于 2013-11-14T18:57:55.833 回答