How do you check the functions defined in $ (document) .ready ()

I am trying to run a simple test with QUnit but will not find the functions defined in $ (document) .ready ().

test.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title></title>
  <script type="text/javascript" src="qunit-git.js"></script>
  <script type="text/javascript" src="jquery.min.js"></script>
  <script type="text/javascript" src="code.js"></script>
  <script type="text/javascript" src="test.js"></script>
  <link rel="stylesheet" type="text/css" href="qunit-git.css" media="screen"/>
</head>
<body>
<h1 id="qunit-header">QUnit Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

test.js

test('Blah blah', function() {
  ok(mytest(), 'foo bar'
  );
});

code.js

$(document).ready(function() {
  var mytest = function() {
    return true;
  }
});

→ "Died in test No. 1: mytest is not defined ..."

+3
source share
1 answer

When you start testing, you may find that you need to improve the structure of your code. Defining functions as local variables inside $ (document) .ready is strange, and makes them impossible to get anywhere, but in a .ready call. Move this function to another location.

+9
source

All Articles