骨干集​​合在取茉莉花+兴农规范不填充 [英] Backbone collection fetch not populating in Jasmine + Sinon spec

查看:123
本文介绍了骨干集​​合在取茉莉花+兴农规范不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行的规格输出我得到预期0等于2. 2是模型对象我在夹具正确的长度,使兴农的fakeServer与嘲笑响应正确响应。我想不通,为什么我收集具有零取的对象后,再。任何帮助将是非常美联社preciated!

When I run this spec output I get "Expected 0 to equal 2." 2 is the correct length of model objects in my fixture so Sinon's fakeServer is responding properly with the mocked response. I can't figure out why my Collection has zero objects after fetch then. Any help would be really appreciated!

FYI:这是沿着骨干兴农+茉莉教程这里来如下:的http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html

FYI: this is coming from following along the Backbone Sinon + Jasmine tutorial here: 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

修改

下面

降压多伊尔帮我看看有没有规范的问题。我有一些问题,与我的茉莉花无头Webkit的配置,如果规格与茉莉花独立运行他们通过。

Buck Doyle below has helped me see there is no spec problem. I have some kind of issue with my Jasmine Headless Webkit config, and if the specs are run with Jasmine standalone they pass.

推荐答案

理论:你需要等待服务器,以检查结果之前对请求做出响应。嘲讽的回应是不够的。在仍是异步的。

Theory: you need to wait for the "server" to respond to the request before checking for the result. Mocking the response isn’t enough: the fetch is still asynchronous.

尝试等待或更复杂的,但优雅的 waitsFor 作为的 https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

Try a waits or a more complicated-but-elegant waitsFor as described at https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

这篇关于骨干集​​合在取茉莉花+兴农规范不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆