【问题标题】:Backbone collection fetch not populating in Jasmine + Sinon specJasmine + Sinon 规范中未填充主干集合提取
【发布时间】:2012-05-09 04:10:44
【问题描述】:

当我运行此规范输出时,我得到“预期 0 等于 2”。 2 是我的夹具中模型对象的正确长度,因此 Sinon 的 fakeServer 正确响应了模拟响应。我无法弄清楚为什么我的 Collection 在 fetch 之后有零个对象。任何帮助将不胜感激!

仅供参考:这是来自跟随此处的 Backbone Sinon + Jasmine 教程:http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html

规格:

describe "Todos collection", ->

  describe "when fetching models from the server", ->
    beforeEach ->
      @todo = sinon.stub(window, "Todo")
      @todos = new Todos()
      @fixture = @fixtures.Todos.valid
      @server = sinon.fakeServer.create()
      @server.respondWith "GET", "/todos", @validResponse(@fixture)

    afterEach ->
      @todo.restore()
      @server.restore()

    it "should parse todos from the response", ->
      @todos.fetch()
      @server.respond()
      expect(@todos.length).toEqual @fixture.response.todos.length

型号:

class window.Todos extends Backbone.Collection
  model: window.Todo
  url: "/todos"
  comparator: (todo) ->
    todo.get('priority')
  parse: (res) ->
    res.response.todos

编辑:

下面的 Buck Doyle 帮助我看到没有规格问题。我的 Jasmine Headless Webkit 配置存在某种问题,如果规范与 Jasmine 独立运行,则它们通过了。

【问题讨论】:

  • 谁有@语法含义的链接?

标签: backbone.js fetch jasmine sinon


【解决方案1】:

理论:在检查结果之前,您需要等待“服务器”响应请求。模拟响应是不够的:fetch 仍然是异步的。

尝试waits 或更复杂但优雅的waitsFor,如https://github.com/pivotal/jasmine/wiki/Asynchronous-specs 所述

【讨论】:

  • 我希望是这样,但添加waits(5000)(甚至荒谬)并没有帮助。不过谢谢!
  • 嗯。我试图在this fiddle 中复制它,它对我有用。你能分享更多你的实现吗?
  • 谢谢巴克!!你的 jsfiddle 帮助我戒掉了一点。我已经分叉并更新了它here。这是我的完整实现,包括从助手中提取的功能。规格通过!我能想到的唯一区别是我使用的是 Jasmine Headless Webkit。这是朝着正确方向的一个非常好的推动
猜你喜欢
  • 2014-01-11
  • 2012-06-11
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多