QUnit async mockjax
HTML
<script src="http://code.jquery.com/qunit/qunit-1.9.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.9.0.css">
<script src="https://raw.github.com/appendto/jquery-mockjax/master/jquery.mockjax.js"></script>
<body>
<h1 id="qunit-header">
QUnit example</h1>
<h2 id="qunit-banner">
</h2>
<div id="qunit-testrunner-toolbar">
</div>
<h2 id="qunit-userAgent">
</h2>
<ol id="qunit-tests">
</ol>
<div id="qunit-fixture">
test markup, will be hidden</div>
</body>
JavaScript
$(document).ready(function() {
function functionToTest() {
return $.getJSON('/echo/json/', {
json: JSON.stringify({
"won't": "run"
})
});
}
var mockHandlerID = $.mockjax({
url: '/echo/json/',
whatToSay: 'HEYO',
response: function(req) {
this.responseText = JSON.stringify({
hello: $.mockjax.handler(mockHandlerID).whatToSay
});
// or more complex stuff
}
});
// holds a backup of the mockjax handler
var _oldhandler;
module("reverse", {
setup: function() {
//keep a copy of the old handler settings
_oldhandler = $.extend({}, $.mockjax.handler(mockHandlerID));
// Make a copy of the mock settigns for use in this module
// Also add default settings for module to it.
$.mockjax.handler(mockHandlerID).response = function(req) {
this.responseText = JSON.stringify({
hello: 'OYEH'
});
};
},
teardown: function() {
$.mockjaxClear(mockHandlerID);
mockHandlerID = $.mockjax(_oldhandler);
}
});
asyncTest("tset tsrif", function() {
functionToTest().done(function(json) {
equal(json && json.hello, 'OYEH');
$.mockjax.handler(mockHandlerID).response = (function() {
this.responseText = JSON.stringify({
hello: 'OtherText'
});
});
functionToTest().done(function(json) {
equal(json && json.hello, 'OtherText');
start();
});
});
});
asyncTest("tset dnoces", function() {
functionToTest().done(function(json) {
equal(json && json.hello, 'OYEH');
start();
});
});
module("normal"); //back to default settings
test("second...