Javascript 如何在js测试中使用mock?
我尝试使用QUnit测试我的javascript代码。 我有一些简单的功能:Javascript 如何在js测试中使用mock?,javascript,jquery,testing,qunit,sinon,Javascript,Jquery,Testing,Qunit,Sinon,我尝试使用QUnit测试我的javascript代码。 我有一些简单的功能: function Multiply(a, b) { return a * b; function CalculateBodyMassIndex(weight, height, using) { return Math.round(weight / (using || Multiply)(height, height)); } } } } 我有两个问题: 1) 我如何验证函数CalculateBodyM
function Multiply(a, b) {
return a * b;
function CalculateBodyMassIndex(weight, height, using) {
return Math.round(weight / (using || Multiply)(height, height));
}
}
}
}
我有两个问题:
1) 我如何验证函数CalculateBodyMassIndex是否被称为乘法函数
2) 如何验证函数中的SummAll将从jQuery库中调用$。个
感谢等待答案。最容易利用函数作用域,并在可模拟函数上允许可选参数:
function Multiply(a, b) {
return a * b;
function CalculateBodyMassIndex(weight, height, using) {
return Math.round(weight / (using || Multiply)(height, height));
}
然后在你的测试中:
var MockMultiplyRan = false;
var MockMultiply = function(a,b) {
MockMultiplyRan = true;
return Multiply(a,b);
}
CalculateBodyMassIndex(200,200, MockMultiply); // arbitrary figures
ok(MockMultiplyRan);
这是一篇关于如何将sinon.js与QUnit一起用于模拟的优秀文章 sinon中的间谍和存根允许您非常轻松地验证对现有对象的调用 编辑
这里的sinon.js文档展示了如何使用Spies。浏览完整的API文档以获取存根、模拟等示例。只需粘贴以下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>
测试
函数乘法(a,b){
返回a*b;
}
函数计算器BdyMassindex(重量、高度){
返回数学圆(重量/乘以(高度、高度));
}
函数总和(数组){
var总和=0;
$.each(数组、函数(i、el){
总和=总和+el;
});
返回总和;
}
$(函数(){
测试('Multiply',function(){
元。每个([
[[0, 2], 0],
[[3, 3], 9],
[[7, 9], 63]
],函数(){
var t=this.toString();
相等的(
乘法。应用(这个,这个[0]),这个[1],
t+':乘以ok'
);
});
});
测试('CalculateBodyMassIndex',函数(){
this.spy(窗口“乘法”);
元。每个([
[[1, 2], 0],
[[10, 2], 3],
[[35, 3], 4]
],功能(i){
var t=this.toString();
相等的(
CalculateBodyMassIndex.apply(这个,这个[0]),这个[1],
t+':CalculateBodyMassIndex正常'
);
ok(Multiply.calledOnce,t+':调用Multiply ok');
深度平等(
Multiply.args[0],[this[0][1],this[0][1]],
t+':乘法参数ok'
);
Multiply.reset();
});
});
测试('SummAll',函数(){
元。每个([
[[1, 2, 3, 4, 5], 15],
[[2, 3, 4, 5, 6], 20],
[[3, 4, 5, 6, 7], 25]
],函数(){
var t=this.toString();
相等的(
SummAll(此[0]),此[1],
t+':总和正常'
);
});
});
});
Hi。谢谢你的回答和链接。我有第二个问题。如果我想确保使用特定参数调用该函数,我应该如何编写测试?不幸的是,我在文章中找不到例子。谢谢你的回答。在这个回答中有一件事让我感到尴尬。我必须在代码中更改所有名为CalculateBodyMassIndex的内容,不是吗?结果我混合了工作逻辑和测试逻辑。我认为这是错误的,因为这是一个不可靠的原则。请你现在给我解释一下好吗?