Skip to content

Instantly share code, notes, and snippets.

@glyons
Created September 6, 2017 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glyons/70a42a513fc434d1b4a9178e3b0aeb8f to your computer and use it in GitHub Desktop.
Save glyons/70a42a513fc434d1b4a9178e3b0aeb8f to your computer and use it in GitHub Desktop.
Javascript QUnit - Fake GET Test using SinonJS
QUnit.test("Javascript QUnit - Fake GET Test using SinonJS", function (assert) {
var url = "http://someurltofake";
var xhr = sinon.useFakeXMLHttpRequest();
var requests = sinon.requests = [];
xhr.onCreate = function (request) {
requests.push(request);
};
var callback = sinon.spy();
$.get(url).success(callback);
assert.equal(sinon.requests.length, 1);
assert.equal(sinon.requests[0].url, url);
requests[0].respond(200, { "Content-Type": "application/json" }, "[]");
assert.ok(callback.called);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment