QUnit async mockjax
by Radhika Magaji
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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.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"
})
});
}
module("first");
test("first test", function() {
stop();
$.mockjax({
url: '/echo/json/',
responseText: JSON.stringify({
hello: 'HEYO!'
})
});
functionToTest().done(function(json) {
ok(true, json.hello);
start();
});
});
test("second test", function() {
stop();
$.mockjaxClear();
$.mockjax({
url: '/echo/json/',
responseText: JSON.stringify({
hello: 'HELL NO!'
})
});
functionToTest().done(function(json) {
ok(true, json.hello);
start();
});
});
});