【问题标题】:Jquery Unit Testing with Qunit and JSMockito (or other framework?), output isn't working as expected使用 Qunit 和 JSMockito(或其他框架?)进行 Jquery 单元测试,输出未按预期工作
【发布时间】:2011-05-03 16:26:01
【问题描述】:

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

【问题讨论】:

    标签: javascript unit-testing mocking mockito qunit


    【解决方案1】:

    两个问题:

    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) 你需要将 JsHamcrest 和 JsMockito 都包含到 QUnit 中,通过运行适当的 javascript 函数(最好在你的脚本块中):

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

    干杯, 克里斯

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2010-11-16
      相关资源
      最近更新 更多