Skip to content

Instantly share code, notes, and snippets.

@juanghurtado
Last active December 17, 2015 13:39
Show Gist options
  • Save juanghurtado/5618659 to your computer and use it in GitHub Desktop.
Save juanghurtado/5618659 to your computer and use it in GitHub Desktop.
Sinon fake server making fixtures fail on another spec file!
define [
'backbone'
'framework/entities/entities'
], (Backbone) ->
beforeEach ->
# Start the sandbox, telling it to use a fake server
@sandbox = sinon.sandbox.create()
@sandbox.useFakeServer()
afterEach ->
@sandbox.restore() # I'm restoring the sandbox, so the fake server is retored too!
describe "Sample spec", ->
it "using Sinon fake server is making my fixtures fail on other spec files :(", ->
# Sinon JS documentation tells to use sandbox.server, but it does not exist.
# I'm using sandbox.serverPrototype instead
@sandbox.serverPrototype.respondWith("GET", "/test", [
200,
{
"Content-Type": "application/json"
},
'{}'
])
# This is just a sample spec. I'm overwriting Backbone.sync method
# to add a _fetchPromise property on the entity on .fetch()
class Mock extends Backbone.Model
url: '/test'
mockEntity = new Mock
mockEntity.fetch()
should = require('chai').should()
should.exist mockEntity._fetchPromise
@juanghurtado
Copy link
Author

Even though fake server is being restored (in fact, sandbox is being restored), XMLHttpRequest still is a FakeXMLHttpRequest object, even on other spec files.

@juanghurtado
Copy link
Author

Forget it. It's my fault. beforeEach and afterEach blocks are not inside describe, so they are global for all specs.

So sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment