-
-
Save cjohansen/739589 to your computer and use it in GitHub Desktop.
/* | |
Load Sinon.JS in the SpecRunner: | |
<script type="text/javascript" src="lib/jasmine-1.0.1/jasmine.js"></script> | |
<script type="text/javascript" src="lib/jasmine-1.0.1/jasmine-html.js"></script> | |
<script type="text/javascript" src="sinon-1.0.0.js"></script> | |
<script type="text/javascript" src="sinon-ie-1.0.0.js"></script> | |
http://cjohansen.no/sinon/ | |
*/ | |
describe("SinonFakeServerWithJasmine", function() { | |
var server; | |
beforeEach(function() { | |
server = sinon.fakeServer.create(); | |
}); | |
afterEach(function () { | |
server.restore(); | |
}); | |
it("should fake a jQuery ajax request", function () { | |
server.respondWith("GET", "/something", | |
[200, { "Content-Type": "application/json" }, | |
'{ "stuff": "is", "awesome": "in here" }']); | |
var callbacks = [sinon.spy(), sinon.spy()]; | |
jQuery.ajax({ | |
url: "/something", | |
success: callbacks[0] | |
}); | |
jQuery.ajax({ | |
url: "/other", | |
success: callbacks[1] | |
}); | |
console.log(server.requests); // Logs all requests so far | |
server.respond(); // Process all requests so far | |
expect(callbacks[0].calledOnce).toBeTruthy(); | |
expect(callbacks[0].calledWith({ | |
stuff: "is", | |
awesome: "in here" | |
})).toBeTruthy(); | |
expect(callbacks[1].calledOnce).toBeFalsy(); // Unknown URL /other received 404 | |
}); | |
}); |
Yup!
I create a fake server but in developer tools in the network tab I see the ajax request hitting the real server. Is that OK?
Are you sure you have defined your routes correctly? One or more of the API calls you are making might not match the pattern specified in a server.respondWith(...) specification.
Yes I get back the response I set up in the respondWidth() so it works fine. But I still see ajax request to the server (which causes my phantomjs/jasmine tests to fail)
Can you put up a gist of your code?
Is it possible to fake one url but not another one?
I have one method call that does two ajax calls. I want to fake only one ajax call.
@rodvlopes - see this gist: https://gist.github.com/jonnyreeves/d8ebad4aaaa7d9c7ae15
I'm using this code you have written above, but I can't seem to get rid of a 'jQuery is not defined' error. Any ideas or solutions to fix this?
Even though I am using CoffeeScript, Mocha, Chai and SinonJS, it shouldn't make a difference.
this doesn't work...
sinonFakeServer
[]
1) should fake a jQuery ajax request
0 passing (137ms)
1 failing
1) sinonFakeServer should fake a jQuery ajax request:
Error: Expected false to exist
at assert (node_modules/expect/lib/assert.js:29:9)
at Expectation.toExist (node_modules/expect/lib/Expectation.js:52:28)
at Context.<anonymous> (test/components/Foo.spec.js:38:35)
I used this code with a little modification; i made the xmlhttprequest call from the test file using the instance of the class where the htpp call is written. But it is not calling my callback function. Any idea why is that happening?
Is it possible to place all the server code in another file, maybe "fakeServer.js," and then still have the fake responses returned when the ajax calls are made from the Jasmine spec?