我刚接触Javascript中的单元测试,并且在将我对单元测试的理解从Java转换到Javascript时遇到了一些麻烦。我有下面的代码,我正在尝试做,基本上我只是想模拟divide函数。目前,当我像这样尝试它时,它只显示没有要运行的测试。我对使用不同的模仿框架的建议持开放态度,我已经看了一些教程,但似乎就是不能理解javascript模仿。任何建议都是值得感谢的。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script type="text/javascript" src="http://witnesstreefiles.s3.amazonaws.com/development/jsmockito-1.0.3-minified.js"></script>
<script>
function divide(a,b)
{
return a / b;
}
$(document).ready(function(){
mockFunc = mockFunction();
when(mockFunc)(anything()).then(function(arg) {
return "foo ";
});
divide = mockFunc
module("Basic Unit Test");
test("Sample test", function()
{
expect(1);
equals(divide(4,2),
2,
'Expected 2 as the result, result was: ' + divide(4,2));
});
});
</script>
</head>
<body>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>发布于 2011-05-04 07:32:40
两个问题:
1)您需要包含JsHamcrest:http://jshamcrest.destaquenet.com/。
<script type="text/javascript" src="https://github.com/downloads/danielfm/jshamcrest/jshamcrest-0.5.2-minified.js"></script>2)您需要通过运行适当的javascript函数(最好是在脚本块中)将JsHamcrest和JsMockito都包含到QUnit中:
JsHamcrest.Integration.QUnit();
JsMockito.Integration.QUnit();干杯,克里斯
https://stackoverflow.com/questions/5872600
复制相似问题