当我运行此规范输出时,我得到“预期0等于2”。 2是我的fixture中模型对象的正确长度,因此Sinon的fakeServer正在使用模拟响应正确响应。我无法弄清楚为什么我的Collection在获取之后没有对象。任何帮助都会非常感激!
仅供参考:这是来自以下的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
答案 0 :(得分:2)
理论:在检查结果之前,您需要等待“服务器”响应请求。模拟响应是不够的:fetch
仍然是异步的。
按https://github.com/pivotal/jasmine/wiki/Asynchronous-specs
所述,尝试waits
或更复杂但更优雅的waitsFor