使用 Qunit 和 JSMockito(或其他框架?)进行 Jquery 单元测试,输出未按预期工作

发布于 11-04 11:13 字数 1693 浏览 5 评论 0原文

我是 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗2024-11-11 11:13:35

两个问题:

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();

干杯,
克里斯

Two problems:

1) You need to include JsHamcrest: http://jshamcrest.destaquenet.com/
E.g.

<script type="text/javascript" src="https://github.com/downloads/danielfm/jshamcrest/jshamcrest-0.5.2-minified.js"></script>

2) You need to include both JsHamcrest and JsMockito into QUnit, by running the appropriate javascript functions (inside your script block is best):

JsHamcrest.Integration.QUnit();
JsMockito.Integration.QUnit();

cheers,
chris

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文