我刚刚尝试为我的javascript函数设置Q-Unit Testing。但是,我似乎遇到了Global Failure
错误在我的JS文件中:script.js
中,我必须将测试代码置于所有我的JS代码之上,否则单元测试将无法运行,而我只会得到全局故障消息。但是,如果我将单元测试粘贴到所有JS函数之上,则虽然可以运行测试,但仍然会遇到全局故障错误。
有人可以引导正确的方向。
test-script.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Q-Unit Tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.10.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.10.0.js" integrity="sha256-X1fQXHSYGxa4V2bqkEAQW0DQGSxJrKveasahr959o28=" crossorigin="anonymous"></script>
<script src="script.js"></script>
</body>
</html>
script.js:
function square(x) {
return x * x;
}
QUnit.test( "square", function( assert ) {
var result = square(2);
assert.equal( result, "4", "square(2) should be 4." );
});
//Normal JS functions are below here
我想通过提供包含jQuery的CDN链接的方式来发布此问题的答案,该CDN链接已解决了全局失败错误,因为它不再出现。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>