Jasmine + Sinon 规范中未填充主干集合提取 [英] Backbone collection fetch not populating in Jasmine + Sinon spec

查看:22
本文介绍了Jasmine + Sinon 规范中未填充主干集合提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

仅供参考:这是来自此处的 Backbone Sinon + Jasmine 教程: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

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

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.

推荐答案

理论:你需要等待服务器"响应请求,然后再检查结果.模拟响应是不够的:fetch 仍然是异步的.

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.

尝试 waits 或更复杂但优雅的 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

这篇关于Jasmine + Sinon 规范中未填充主干集合提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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