【问题标题】:Testing Titanium Alloy BackboneJS Collection with sample data使用样本数据测试 Titanium Alloy BackboneJS Collection
【发布时间】:2013-11-12 17:41:46
【问题描述】:

我试图弄清楚如何使用示例数据测试 Backbone.Collection,但每次我在 Collection 上调用 fetch 时,它的行为似乎都不像我期望的那样。比如使用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

【问题讨论】:

  • 获取是异步的。

标签: backbone.js coffeescript appcelerator titanium-alloy


【解决方案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()

【讨论】:

  • 嗨博蒂。抱歉,我认为这解决了我的问题,但我似乎根本没有收到回调
  • 我已经尝试运行 collection.fetch view, success: => Ti.API.warn "this is a test"collection.fetch {view: "default", success: => Ti.API.warn "this is a test"} 并且似乎没有任何回调被触发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 2015-06-24
  • 2015-07-05
相关资源
最近更新 更多