使用 Qunit 和 JSMockito(或其他框架?)进行 Jquery 单元测试,输出未按预期工作
我是 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>
I'm new to unit testing in Javascript and am having some trouble adapting my understanding of unit testing from Java to Javascript. I have the code below that I am trying to do, basically I just want to mock the divide
function. Currently, when I try it like this it just says there are no tests to run. I am open to suggestions of using a different mocking framework, I have been looking at a number of tutorials but just can't seem to wrap my head around javascript mocking. Any advice is appreciated.
<!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>
两个问题:
1)您需要包含 JsHamcrest: http://jshamcrest.destaquenet.com/
例如
2) 您需要通过运行适当的 javascript 函数(最好在脚本块内)将 JsHamcrest 和 JsMockito 包含到 QUnit 中:
干杯,
克里斯
Two problems:
1) You need to include JsHamcrest: http://jshamcrest.destaquenet.com/
E.g.
2) You need to include both JsHamcrest and JsMockito into QUnit, by running the appropriate javascript functions (inside your script block is best):
cheers,
chris