37-解决方案
12.9.2 解决方案
为jQuery插件编写单元测试的最简单方法是利用QUnit,这是jQuery项目使用的同一个单元测试框架。可以用QUnit编写JavaScript测试,然后把它们和插件一同交付给用户,在他们自己的浏览器中运行:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml " xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="../jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="metadata/jquery.metadata.js"></script>
<script type="text/javascript" src="jquery.pulse.js"></script>
<link rel="stylesheet"
href="http://jqueryjs.googlecode.com/svn/trunk/qunit/testsuite.css " type="text/css"
media="screen" />
<script type="text/javascript"
src="http://jqueryjs.googlecode.com/svn/trunk/qunit/testrunner.js "></script>
</head>
<body>
<script type="text/javascript">
module("Testing the jQuery Pulse Plugin");
test("Test Pulse with basic options", function() {
$("div.starship").pulse();
equals($("#enterprise").css("opacity"),1,"The element should be visible");
equals($("#galactica").css("opacity"),1,"The element should be visible");
});
test("Test Impulse", function() {
$.pulse.impulse($("#galactica"));
equals($("#galactica").css("opacity"),1,"The element should be visible");
});
test("Test Warp Speed", function() {
$.pulse.warpspeed($("#enterprise"));
equals($("#enterprise").css("opacity"),1,"The element should be visible");
});
</script>
<div id="main">
<div class="starship" id="enterprise">USS Enterprise - NC-1701-A</div>
<div class="starship" id="galactica">Battlestar Galactica</div>
</div>
</body>
</html>