【问题标题】:Restoring an anonymous sinon.stub恢复匿名 sinon.stub
【发布时间】: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>

【问题讨论】:

    标签: jquery qunit sinon


    【解决方案1】:

    如果您使用的是沙盒,您可以自动恢复存根。这是通过将您的测试用例函数包装在对sinon.test(...) 的调用中来完成的。

    更多详情请查看Sinon沙箱文档:http://sinonjs.org/docs/#sandbox

    【讨论】:

      【解决方案2】:

      很遗憾,Sinon 不会自动为您恢复已存根的方法。

      您在测试中所做的是用 Sinon 存根函数覆盖全局 cycle。您不要保留旧的进行恢复,然后在测试结束时恢复它,您需要这样做。

      例如:

      test("Double click LO triggers cycle function", function (assert) {
          var origCycle = cycle;
          cycle = sinon.stub();
          $('img#LO8').dblclick();
          assert.equal(cycle.calledOnce, true, 'cycle called after dblclick on LO');
          cycle = origCycle;
      });
      

      您可能对sinon.stub(obj, "method") 语法感到困惑,它允许您在测试结束时调用obj.method.restore() 来恢复原始方法,但您仍然需要自己手动恢复。

      不过,如果您愿意,也可以使用该语法:

      test("Double click LO triggers cycle function", function (assert) {
          sinon.stub(window, 'cycle');
          $('img#LO8').dblclick();
          assert.equal(cycle.calledOnce, true, 'cycle called after dblclick on LO');
          window.cycle.restore(); // or even just `cycle.restore()`
      });
      

      【讨论】:

      • 我认为您的回答有点误导,因为这意味着Sinon通常不支持自动恢复存根。但是您可以使用沙盒在测试用例之后自动恢复存根和间谍。
      • @mantoni 令人着迷的是,我根本不知道sinon.test() 允许自动恢复存根和间谍。感谢您提请我注意。
      • 我认为匿名存根不需要恢复,因为它们是匿名的。它们不会取代任何东西的功能。对吗?
      • @cameronjroe 不完全是。在 OP 的示例中,有一个全局变量 cycle 被重新分配给由 sinon.stub() 创建的匿名存根函数。因此,当运行该测试时,cycle 的原始值会丢失,并且无法将其设置回原始函数。使用这里提到的其他技术可以解决这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2014-09-11
      相关资源
      最近更新 更多