Ho una specifica di raccolta Backbone (che ovviamente utilizza anche Sinon per falsificare la risposta del server con i dati delle fixture. Questo passa con Jasmine standalone, ma fallisce sotto il mio setup di Jasmine Headless Webkit.Sinon + Backbone spec che non funziona con Jasmine Headless Webkit ma passa con Jasmine standalone
Il fallimento è questo:
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 e la mia gara di ritorno correttamente un reposonse falso con 2 todos, ma nessun todos vengono analizzati nella collezione.
C'è un violino che mostra tutto questo lavoro con Jasmine standalone here
mio ordine di caricamento Jasmine Headless Webkit è il seguente:
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
E il codice (anche il violino, ovviamente):
# 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
Diversi degli strumenti in uso sono nuovi per me quindi apprezzo davvero qualsiasi aiuto-- grazie !!
FYI, sono stato in grado di ricreare questo. –
Grazie per aver esaminato questo! Apprezzo il vostro aiuto. – Blastula