0

我在为下面的小脚本创建测试时遇到了麻烦。我有 3 个测试和测试 nr 3 我想为我的变量赋值:

var myWeight;
var myDistance;
var mySpeed;

然后计算函数差异,看看它是否正确答案。我在 qunit 中收到此错误:

        Tere tere tere tere
        Expected:   

    "531.36"

    Result:     

    NaN

    Diff:   

    "531.36" NaN 

资源:
@file:///C:/Users/John%20Wayne/SkyDrive/Documents/testimine2.html:128

  <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>QUnit basic example</title>
    <link rel="stylesheet" href="qunit.css">
    </head>
    <body>
    <div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="qunit.js"></script>

<SCRIPT LANGUAGE="JavaScript">

<!--
var myWeight;
var myDistance;
var mySpeed;

function HowMany(form)
{
var difference;
difference = (myDistance * myWeight * mySpeed) * .123;
form.Fdiff.value = difference;

if (difference < 1) {
form.comment.value="Error, please check your answers";
}
if (difference > 1 && difference < 100) {
form.comment.value="You better start working!";
}
if (difference > 101 && difference < 200) {
form.comment.value="Nice run, but you can do better.";
}
if (difference > 201 && difference < 300) {
form.comment.value="Very good! Push above 300 next time.";
}
if (difference > 301 && difference < 500) {
form.comment.value="Great! Your a runner.....keep it up!";
}
if (difference > 501 && difference < 700) {
form.comment.value="Bill Rogers move over!";
}
if (difference > 701 && difference < 1200) {
form.comment.value="Your my hero! Have a jelly doughnut."; 
}

if (difference > 1201 && difference < 2500) {
    form.comment.value="You are killing yourself, its too much!";
}
if (difference > 2500) {
    form.comment.value="Please check your input, the output is too high but not impossible";
}
}

function SetMyWeight(weight)
{
myWeight = weight.value;
}

function SetmyDistance(dis)
{
myDistance = dis.value;
}
function SetMySpeed(speed)
{
mySpeed = speed.value
}

function ClearForm(form){

form.myWeight.value = "";
form.myDistance.value = "";
form.mySpeed.value = "";
form.Fdiff.value = "";
form.comment.value = "";

}
// -->

</SCRIPT>
<center>
<FORM METHOD="POST">
<TABLE border=3>
<TR>
<TR>
<TD><div align=center>Your<br>Weight</div></TD>
<TD><div align=center>Miles<br>run</div></TD>
<TD><div align=center>Speed<br>Km/H</div></TD>
<TD><div align=center>Calories<br>burned</div></TD>
<TD><INPUT TYPE=BUTTON ONCLICK="HowMany(this.form)" VALUE="Calculate"></TD>
</TR>
<tr>
<TD><div align=center><INPUT TYPE=text NAME=myWeight SIZE="4"ONCHANGE="SetMyWeight(this)"></div></TD>
<TD><div align=center><INPUT TYPE=text NAME=myDistance SIZE="4"ONCHANGE="SetmyDistance(this)"></div></TD>
<TD><div align=center><INPUT TYPE=text NAME=mySpeed SIZE="4"ONCHANGE="SetMySpeed(this)"></div></TD>
<TD><div align=center><INPUT TYPE=text NAME="Fdiff" VALUE="" SIZE="6"></div></TD>
<TD><div align=center><INPUT TYPE=BUTTON VALUE=" Reset " onClick="ClearForm(this.form)"></div></tr>
</table>
<table border=3>
<tr>
<TD><DIV ALIGN=CENTER>My Comment</DIV></TD>
<TD><INPUT TYPE=text NAME="comment" size="37"></td>
</TR>
</TABLE>
</FORM>
</center>


<script>
test( "a basic test example", function() {
var value = "hello";
equal( value, "hello", "We expect value to be hello" );
});
</script>
<script>
    test( "Too few calories", function() {
        difference = "67";
        equal( difference, "67", "You better start working!")    
    });
</script>
<script>
    test( "Kaalu sisestamine", function() {
        MyWeight = "54"
        MyDistance = "10"
        mySpeed = "8"
        difference = (myDistance * myWeight * mySpeed) * .123;
        equal(difference, "531.36", "Tere tere tere tere");
    });

</body> 
</html>
4

1 回答 1

0

您做得非常正确,问题在于 JavaScript 是一种区分大小写的语言。变量定义为:

var myWeight;
var myDistance;
var mySpeed;

并且除了第三次测试之外,您在任何地方都可以正确使用它们。你有:

MyWeight = "54"
MyDistance = "10"

但应该是

myWeight = "54"
myDistance = "10"

如果您确保使用正确的变量名(包括大小写正确性),那么您应该没问题。

其他一些通用编码技巧:

  • 您在第三次测试结束时缺少一个标签。
  • 也就是说,您可以在一组脚本标签中一个接一个地运行所有测试。没有必要将它们全部放在自己的一组脚本标签中,这只是没有意义的额外代码。

此外,您并没有真正按照预期的方式使用 QUnit。您应该考虑将表格移动到 qunit-fixture div 中,并通过 DOM 操作实际使用输入元素来测试实际表单。通过更改您正在测试的脚本中的变量来更改值不是好的做法。

于 2013-12-07T07:12:36.067 回答