Backbone.js 使用样本数据测试钛合金BackboneJS采集

Backbone.js 使用样本数据测试钛合金BackboneJS采集,backbone.js,coffeescript,appcelerator,titanium-alloy,Backbone.js,Coffeescript,Appcelerator,Titanium Alloy,我正试图找出如何使用示例数据测试主干.集合,但每次调用集合上的fetch,它的行为似乎与我预期的不一样。例如,使用茉莉花和咖啡脚本 describe "task model", -> testData = [ { id: 1, type: "personal", complete: false } { id: 2, type: "business", complete: true } ] collection = null item = null

我正试图找出如何使用示例数据测试主干.集合,但每次调用集合上的
fetch
,它的行为似乎与我预期的不一样。例如,使用茉莉花和咖啡脚本

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

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

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

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


获取是异步的。嗨,博蒂。很抱歉,我认为已经解决了我的问题,但我似乎根本没有收到回调。我已尝试运行
collection.fetch视图,成功:=>Ti.API.warn“这是一个测试”
collection.fetch{view:“default”,成功:=>Ti.API.warn“这是一个测试”}
并且似乎没有任何回调被触发
collection.fetch view,
  checkFunction = ->
    expect(collection.length).toEqual 2
  success: =>
    checkFunction()
  error: =>
    checkFunction()