Created
August 25, 2011 15:39
-
-
Save commuterjoy/1170957 to your computer and use it in GitHub Desktop.
Qunit + Sinon
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html lang="en"> | |
<head> | |
<title>QUnit/Sinon.JS</title> | |
<link rel="stylesheet" href="vendor/qunit/qunit.css" type="text/css" media="screen"> | |
</head> | |
<body> | |
<h1 id="qunit-header">QUnit/Sinon.JS Test</h1> | |
<h2 id="qunit-banner"></h2> | |
<h2 id="qunit-userAgent"></h2> | |
<ol id="qunit-tests"></ol> | |
<script src="vendor/jquery/jquery.min.js"></script> | |
<script src="vendor/qunit/qunit.js"></script> | |
<script src="vendor/sinon/sinon.js"></script> | |
<script src="vendor/sinon/sinon-qunit-1.0.0.js"></script> | |
<script> | |
// @seealso https://gist.github.com/739589 | |
test("spying", function(){ | |
// a call back object + method | |
var foo = { | |
bar: function(){ | |
console.log(1); | |
} | |
} | |
// spy both on the callback and the xhr object | |
var spy = sinon.spy(foo, "bar") | |
var spy = sinon.spy(jQuery, "ajax") | |
// check everything works ok | |
foo.bar(); | |
ok(foo.bar.calledOnce, "foo bar called once"); | |
// use a fake XHR | |
var xhr = sinon.useFakeXMLHttpRequest(); | |
// mock a server response | |
var server = sinon.fakeServer.create(); | |
server.respondWith("GET", "/foo", [200, {}, 'some server data']); | |
// enqueue a request | |
var b = jQuery.ajax('/foo', { success: function(data, textStatus, jqXHR){ | |
console.log('woo'); | |
foo.bar(); | |
}, | |
error: function(jqXHR, textStatus, errorThrown){ | |
console.log('boo'); | |
}, | |
complete: function(){ | |
console.log('done'); | |
} | |
}); | |
// check the request has been sent | |
ok(jQuery.ajax.calledOnce, "jQuery.ajax called once"); | |
// dequeue the request | |
server.respond(); | |
// test the success method has fired | |
ok(foo.bar.calledTwice, "foo bar called twice"); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment