Unit Testing Ajax with Sinon.js and QUnit
HTML
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="http://sinonjs.org/releases/sinon-1.7.3.js"></script>
<script src="http://sinonjs.org/releases/sinon-qunit-1.0.0.js"></script>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
JavaScript
test("data from server", function () {
var server = this.sandbox.useFakeServer();
server.respondWith("GET", "/mytest",
[200, { "Content-Type": "application/json" },
'[{ "id": "52", "name": "Rock" }]']);
var callback = this.spy();
$.ajax({
url: "/mytest",
success: callback
});
server.respond();
equal(server.requests.length, 1, "server request length");
ok(callback.called, "spy called once"); //failed
ok(callback.calledWith([{ id: "52", name: "Rock" }])); //failed
});