I'm using Qunit to unit test some jQuery. For some of my tests, I want to stub out a function (called cycle()), and for later tests, I want to call the original function. I've organised and separated these in modules, but the stub is still in place. I thought from the sinon.js docs, the original function would be restored (removing the stub) once a test was finished, and certainly after moving to a new module.
Part of the difficulty seems to be that I have used an anonymous stub, thus making it difficult to simply call stub.restore.
Any advice? Thanks.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript tests</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit 1.17.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
<img id="logo" src="" width=100 height=100 alt="dummy logo">
<ol>
<li class="criterion">
<img id="gfx" src="" width=100 height=100 alt="dummy graphic">
<img id="SC1" class="tl-red"
src="../../../static/images/img_trans.png">Some text blurb
</li>
</ol>
<ol>
<li class="learning_outcome">
<img id="LO8" class="tl-red"
src="../../../static/images/img_trans.png" >More blurb...
</li>
</ol>
</div>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.17.1.js"></script>
<script src="../../../static/sinon.js"></script>
<script src="http://sinonjs.org/releases/sinon-qunit-1.0.0.js"></script>
<script src="../../../static/jQplugins/carhartl-jquery-cookie-fedc4cb/jquery.cookie.js"></script>
<script src="../../../static/traffic.js"></script><!--cycle defined in there-->
<script>
/*global $, test, equal */
module("Traffic light is bound to cycle function");
test("Double click LO triggers cycle function", function (assert) {
cycle = sinon.stub();
$('img#LO8').dblclick();
assert.equal(cycle.calledOnce, true, 'cycle called after dblclick on LO');
});
module("Cycle function tests");
test("The cycle function requires img argument", function (assert) {
assert.throws(function() {
cycle(); /*This is still stubbed out, but why? */
},
Error,
'passing no image throws Error');
});
</script>
</body>
</html>