只需粘贴以下 html 并从浏览器中打开它。
<html>
<head>
<title>test</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<script src="http://sinonjs.org/releases/sinon-1.6.0.js"></script>
<script src="http://sinonjs.org/releases/sinon-qunit-1.0.0.js"></script>
<script type="text/javascript">
function Multiply(a, b) {
return a * b;
}
function CalculateBodyMassIndex(weight, height) {
return Math.round(weight / Multiply(height, height));
}
function SummAll(array) {
var summ = 0;
$.each(array, function (i, el) {
summ = summ + el;
});
return summ;
}
</script>
</head>
<body>
<div id="qunit"></div>
<script type="text/javascript">
$(function(){
test('Multiply', function(){
$.each([
[[0, 2], 0],
[[3, 3], 9],
[[7, 9], 63]
], function(){
var t = this.toString();
equal(
Multiply.apply(this, this[0]), this[1],
t + ': Multiply ok'
);
});
});
test('CalculateBodyMassIndex', function(){
this.spy(window, 'Multiply');
$.each([
[[1, 2], 0],
[[10, 2], 3],
[[35, 3], 4]
], function(i){
var t = this.toString();
equal(
CalculateBodyMassIndex.apply(this, this[0]), this[1],
t + ': CalculateBodyMassIndex ok'
);
ok(Multiply.calledOnce, t + ': call Multiply ok');
deepEqual(
Multiply.args[0], [this[0][1], this[0][1]],
t + ': Multiply args ok'
);
Multiply.reset();
});
});
test('SummAll', function(){
$.each([
[[1, 2, 3, 4, 5], 15],
[[2, 3, 4, 5, 6], 20],
[[3, 4, 5, 6, 7], 25]
], function(){
var t = this.toString();
equal(
SummAll(this[0]), this[1],
t + ': SummAll ok'
);
});
});
});
</script>
</body>
</html>