【发布时间】:2015-05-31 03:46:39
【问题描述】:
我正在使用 Qunit 对一些 jQuery 进行单元测试。对于我的一些测试,我想存根一个函数(称为cycle()),而对于以后的测试,我想调用原始函数。 我已经在模块中组织和分离了这些,但存根仍然存在。我从 sinon.js 文档中认为,一旦测试完成,原始函数将被恢复(删除存根),当然是在移动到新模块之后。
部分困难似乎是我使用了匿名存根,因此很难简单地调用 stub.restore。
有什么建议吗?谢谢。
<!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>
【问题讨论】: