Sinon + Backbone规范在Jasmine Headless Webkit下失败,但通过独立的Jasmine传递

时间:2012-05-10 15:39:24

标签: backbone.js jasmine sinon

我有一个Backbone收集规范(当然是todos),它使用Sinon伪造服务器响应和夹具数据。这通过独立的Jasmine但在我的Jasmine Headless Webkit设置下失败。

失败的是:

Todos collection when fetching models from the server should parse todos from the response.

  Expected 0 to equal 2. (line ~58)
    expect(this.todos.length).toEqual(this.fixture.response.todos.length);

Sinon和我的装置正确地返回了一个带有2个待办事项的假reposonse,但是在该集合中没有解析todos。

有一个小提琴显示所有这些与Jasmine独立here

一起使用

我的Jasmine Headless Webkit加载顺序如下:

clyde:backbone-jasmine andy$ jasmine-headless-webkit -l
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-core-1.1.0/lib/jasmine-core/jasmine.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-core-1.1.0/lib/jasmine-core/jasmine-html.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-core-1.1.0/lib/jasmine-core/jasmine.css
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/jasmine-extensions.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/intense.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/headless_reporter_result.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/jasmine.HeadlessConsoleReporter.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/jsDump.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-headless-webkit-0.8.4/vendor/assets/javascripts/beautify-html.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jquery-rails-2.0.2/vendor/assets/javascripts/jquery.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jquery-rails-2.0.2/vendor/assets/javascripts/jquery_ujs.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-core-1.1.0/lib/jasmine-core/json2.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/backbone-rails-0.9.2/vendor/assets/javascripts/underscore.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/backbone-rails-0.9.2/vendor/assets/javascripts/backbone.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/backbone-rails-0.9.2/vendor/assets/javascripts/backbone-rails.js
/Users/andy/Code/backbone-jasmine/app/assets/javascripts/app/collections/todos.js.coffee
/Users/andy/Code/backbone-jasmine/app/assets/javascripts/app/models/todo.js.coffee
/Users/andy/Code/backbone-jasmine/app/assets/javascripts/application.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-spec-extras-0.0.2/vendor/assets/javascripts/jasmine-jquery.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-spec-extras-0.0.2/vendor/assets/javascripts/sinon.js
/Users/andy/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jasmine-spec-extras-0.0.2/vendor/assets/javascripts/jasmine-sinon.js
/Users/andy/Code/backbone-jasmine/spec/javascripts/helpers/spec_helper.js.coffee
/Users/andy/Code/backbone-jasmine/spec/javascripts/helpers/valid_response.js.coffee
/Users/andy/Code/backbone-jasmine/spec/javascripts/fixtures/todos_fixture.js.coffee
/Users/andy/Code/backbone-jasmine/spec/javascripts/app/models/todo_spec.js.coffee
/Users/andy/Code/backbone-jasmine/spec/javascripts/app/collections/todos_spec.js.coffee

代码(当然也在小提琴中):

# abbreviated todo model
class window.Todo extends Backbone.Model


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


# spec/javascripts/fixtures/todos_fixture.js.coffee
beforeEach ->
  @fixtures = Todos:
    valid:
      status: "OK"
      version: "1.0"
      response:
        todos: [
          id: 1
          title: "Paint the fence"
        ,
          id: 2
          title: "Wash the dog"
         ]


# spec/javascripts/helpers/valid_response.js.coffee
beforeEach ->
  @validResponse = (responseText) ->
    [ 200, "Content-Type": "application/json",
      JSON.stringify responseText ]


# abbreviated todo spec (full spec in fiddle)
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 ->
      @server.restore()
      @todo.restore()

    it "should parse todos from the response", ->
      @todos.fetch()
      @server.respond()
      expect(@todos.length).toEqual @fixture.response.todos.length
      expect(@todos.get(1).get('title')).toEqual @fixture.response.todos[0].title

使用的一些工具对我来说是新的,所以我非常感谢任何帮助 - 谢谢!!

1 个答案:

答案 0 :(得分:0)

也许在server.respond()中发生了异步。 你试过这个吗?

it "should parse todos from the response", ->
  @todos.fetch()
  @server.respond()
  waits 500
  runs ->
    expect(@todos.length).toEqual @fixture.response.todos.length
    expect(@todos.get(1).get('title')).toEqual @fixture.response.todos[0].title