javascript - QUnit tests failing intermittently -


below javascript file shows sum of 2 numbers.

var getsum = function (arg1, arg2) {     var intarg1 = parseint(arg1);     var intarg2 = parseint(arg2);     return intarg1 + intarg2; };  var getsumtext = function (arg1, arg2) {     var sum = getsum(arg1, arg2);     return 'the sum of ' + arg1 + ' , ' + arg2 + ' ' + sum + '.'; };  $(document).ready(function () {     $("#button1").click(function (e) {         console.log('button clicked');         var sumtext = getsumtext($("#arg1").val(), $("#arg2").val());         $("#output1").text(sumtext);         e.stoppropagation();     }); }); 

here's qunit.html file.

<!doctype html> <html> <head>     <meta charset="utf-8" />     <title>test suite</title>     <link href="content/qunit-1.12.0.css" rel="stylesheet" />     <script src="scripts/jquery-2.0.0.min.js"></script>     <script src="scripts/qunit-1.12.0.js"></script>     <script src="scripts/main.js" data-cover></script>     <script src="scripts/maintests.js"></script>     <script src="scripts/blanket.min.js"></script> </head> <body>     <h1 id="qunit-header">test suite</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">         <input type="text" id="arg1" />&nbsp;         <input type="text" id="arg2" /> <br />         <div id="output1">&nbsp;</div>         <input type="button" id="button1" value="show sum" />     </div>         <script type="text/javascript">function blanket_togglesource(e) {     var t = document.getelementbyid(e);     t.style.display === "block" ? t.style.display = "none" : t.style.display = "block"; }</script> </body> </html> 

and here's js tests file.

module('dom'); test('should add correctly', 1, function() {     $('#arg1').val('2');     $('#arg2').val('5');     console.log($('#arg1').val());     console.log($('#arg2').val());     $('#button1').trigger('click');     var output = $('#output1').text();      equal(output, 'the sum of 2 , 5 7.', 'sum text correct'); });  module('sum'); test('should add correctly', 1, function() {     var sum = getsum('2', '1');     deepequal(sum, 3, 'sum correct'); }); 

if move 'sum' module above 'dom' one, test in dom module fail intermittently. missing?

thanks, arun

you need separate library code, can test, initialization code, don't need test in case. move code inside click handler named function , called separate script tag or file. way can test 3 functions tests. end document-ready executing code @ point in time, while tests run @ another. causes seemingly random behaviour you're seeing.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -