Last active
December 17, 2015 13:39
-
-
Save juanghurtado/5618659 to your computer and use it in GitHub Desktop.
Sinon fake server making fixtures fail on another spec file!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Even though fake server is being restored (in fact, sandbox is being restored), XMLHttpRequest
still is a FakeXMLHttpRequest
object, even on other spec files.
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
I'm using Sinon to fake server requests on my specs. For the fixtures staff I'm using https://github.com/badunk/js-fixtures
When using a sandboxed fake server as seen on the example, fixtures stop working, even though the fake server is being sandboxed and restored.
What am I doing wrong? :S