avoid / capture / verify a Javascript alert when testing a method that displays one with qunit
Asked Answered
F

1

7

I'm just starting using Qunit and would like to know whether is there a way to capture/verify/omit alerts, For example:

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

and then have something like:

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
});

I'm open to the suggestion of using another unit testing tool as well.

Thanks in advance!

Foxtrot answered 7/7, 2011 at 9:10 Comment(0)
A
10

Alright, looks like Sinon.JS is what you are looking for. I've never used it before, but I did to answer your question.

You can replace the global function alert (which is actually window.alert) with a temporary function that will record the message that would have been displayed.

It's easy to do in javascript (window.alert = function(msg) { savedMsg = msg; }). So you could do that within your test.

The complexity comes only from cleaning up after you've run your test. That's where you need Sinon.JS which can integrate with QUnit. You'll need 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>
Abbey answered 7/7, 2011 at 10:9 Comment(5)
Thanks memetech! It works awesome, but still i've a little problem, do you know how to make it work for IE 6? It keeps throwing me the "Object doesn't support this property or method" when I do "this.stub..." =( Thanks!Foxtrot
I'll keep looking but unfortunately I don't have access to IE6 here to reproduce the issue. Do include the script: sinonjs.org/releases/sinon-ie-1.1.1.js , but I don't think that will solve your issue.Abbey
@Foxtrot , it is reproducible in IE8+compatibility mode. On line 84 of the sinon.js script, you should add in && type != "object". IE appears to report built-in functions as type 'object', something Sinon tries to protect against. However, it all works fine. Line 84 should now be: ` if (type != "function" && type != "object") {`Abbey
FYI Christian from sinonjs.org suggests it's not a good idea to remove this protection, and instead you could use window.alert = sinon.spy() within your unit test ( I haven't tried it ). You'd have to use var _savedAlert = window.alert; try { window.alert=sinon.spy(); } finally { window.alert = _savedAlert; } to make sure window.alert is always restored at the conclusion of the test.Abbey
Memetech, I really appreciate your help. This last solution worked perfectly in IE. Now I'll be able to test my app's whole js logic. Best of luck !Foxtrot

© 2022 - 2024 — McMap. All rights reserved.