admin管理员组文章数量:1289845
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!
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!
Share Improve this question asked Jul 7, 2011 at 9:10 SoledadSoledad 852 silver badges6 bronze badges1 Answer
Reset to default 10Alright, 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 plexity es 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./jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery./qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery./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>
版权声明:本文标题:avoidcaptureverify a Javascript alert when testing a method that displays one with qunit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443610a2379063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论