[英]Javascript Testing with QUnit
I want to check my js code via qunit.As i am checking simple js code i know. 我想通过qunit检查我的js代码,因为我正在检查我知道的简单js代码。 Can anybody please tell me that how to check below type of js code with qunit?Answer to this qustion will be really apraisible, 有人可以告诉我如何用qunit检查以下类型的js代码吗?回答这个问题确实很切合实际,
function check_person() {
var no_adt = document.getElementById('adult').value;
var no_chd = document.getElementById('child').value;
var no_inf = document.getElementById('infant').value;
if (no_inf > no_adt) {
alert('Number of Infants must be less than equal to number of Adults');
return false;
}
if (parseInt(no_adt) + parseInt(no_chd) + parseInt(no_inf) > 9) {
alert('Please note there is a maximum of 9 passengers permitted on the search form');
return false;
}`enter code here`
return true;
} }
<script type="text/javascript">
function check_person() {
var no_adt = document.getElementById('adult').value;
var no_chd = document.getElementById('child').value;
var no_inf = document.getElementById('infant').value;
if (no_inf > no_adt) {
alert('Number of Infants must be less than equal to number of Adults');
return false;
}
if (parseInt(no_adt) + parseInt(no_chd) + parseInt(no_inf) > 9) {
alert('Please note there is a maximum of 9 passengers permitted on the search form');
return false;
}
return true;
}
function assignValues(adult, child, infant){
document.getElementById('adult').value = adult;
document.getElementById('child').value = child;
document.getElementById('infant').value = infant;
}
test("Test check_person", function(){
assignValues(5,2,1);
ok(check_person());
assignValues(2,1,5);
ok(!check_person(), "Number of Infants is less than equal to number of Adults");
assignValues(7,2,5);
ok(!check_person(), "a maximum of 9 passengers permitted");
});
</script>
<body>
<div id="qunit-fixture">
<input id="adult" />
<input id="child" />
<input id="infant" />
</div>
<div id="qunit">
</div>
</body>
it's better to modify your function to make it more testable: 最好修改您的功能以使其更具可测试性:
function check_person(no_adt, no_chd, no_inf) {
if (no_inf > no_adt) {
alert('Number of Infants must be less than equal to number of Adults');
return false;
}
if (parseInt(no_adt) + parseInt(no_chd) + parseInt(no_inf) > 9) {
alert('Please note there is a maximum of 9 passengers permitted on the search form');
return false;
}
return true;
}
And then you don't need html markup and assignValues(): 然后,您不需要html标记和AssignValues():
test("Test check_person", function(){
ok(check_person(5, 2, 1));
ok(!check_person(5, 2 ,1), "Number of Infants is less than equal to number of Adults");
ok(!check_person(7, 2, 5), "a maximum of 9 passengers permitted");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.