【问题标题】:avoid / capture / verify a Javascript alert when testing a method that displays one with qunit在测试使用 qunit 显示的方法时避免/捕获/验证 Javascript 警报
【发布时间】:2011-07-07 09:10:37
【问题描述】:

我刚开始使用 Qunit,想知道是否有办法捕获/验证/忽略警报,例如:

function to_test() {
   alert("I'm displaying an alert");
   return 42;
 }

然后有类似的东西:

test("to_test", function() {
  //in this case I'd like to test the alert.
  alerts("I'm displaying an alert", to_test(), "to_test() should display an alert"); 
  equals(42, to_test(), "to_test() should return 42" );  // in this case I'd like to omit the alert
});

我也愿意接受使用其他单元测试工具的建议。

提前致谢!

【问题讨论】:

    标签: javascript alert qunit jsunit


    【解决方案1】:

    好的,看起来Sinon.JS 就是您要查找的内容。我以前从未使用过它,但我确实回答了您的问题。

    您可以将全局函数alert(实际上是window.alert)替换为一个临时函数,该函数将记录本应显示的消息。

    在 javascript (window.alert = function(msg) { savedMsg = msg; }) 中很容易做到。所以你可以在你的测试中做到这一点。

    复杂性仅来自运行测试后的清理。这就是你需要Sinon.JS 的地方,它可以是integrate with QUnit。你需要this integration script

    <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
    <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
    <script type="text/javascript" src="sinon-1.1.1.js"></script>
    <script type="text/javascript" src="sinon-qunit-0.8.0.js"></script>
    
    <script>
    
        function to_test() {
          window.alert("I'm displaying an alert");
          return 42;
        }
    
        $(document).ready(function(){
    
          module("Module A");
    
          test("first skip alert test ", function() {
    
          var stub = this.stub(window, "alert", function(msg) { return false; } );
    
          equals(42, to_test(), "to_test() should return 42" );  
          equals(1, stub.callCount, "to_test() should have invoked alert one time");
          equals("I'm displaying an alert",stub.getCall(0).args[0], "to_test() should have displayed an alert" ); 
    
        });
    
      });
    </script>
    
    </head>
    <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>
    </html>
    

    【讨论】:

    • 感谢 memetech!它工作得很棒,但我仍然有一个小问题,你知道如何让它在 IE 6 上工作吗?当我执行“this.stub ...”时,它不断向我抛出“对象不支持此属性或方法”=(谢谢!
    • 我会继续寻找,但不幸的是我无法在此处访问 IE6 来重现此问题。务必包含脚本:sinonjs.org/releases/sinon-ie-1.1.1.js,但我认为这不会解决您的问题。
    • @Soledad ,在 IE8+兼容模式下可重现。在 sinon.js 脚本的第 84 行,您应该添加 &amp;&amp; type != "object"。 IE 似乎将内置函数报告为“对象”类型,这是 Sinon 试图防止的。但是,一切正常。第 84 行现在应该是:` if (type != "function" && type != "object") {`
    • 来自 sinonjs.org 的仅供参考 Christian 建议删除此保护不是一个好主意,相反,您可以在单元测试中使用 window.alert = sinon.spy()(我还没有尝试过)。您必须使用var _savedAlert = window.alert; try { window.alert=sinon.spy(); } finally { window.alert = _savedAlert; } 确保在测试结束时始终恢复 window.alert。
    • Memetech,非常感谢您的帮助。最后一个解决方案在 IE 中完美运行。现在我将能够测试我的应用程序的整个 js 逻辑。祝你好运!
    猜你喜欢
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    相关资源
    最近更新 更多