【问题标题】:How do I use qUnit to test a javascript print method?如何使用 qUnit 测试 javascript 打印方法?
【发布时间】:2012-08-27 01:03:40
【问题描述】:

如何使用 qUnit 测试 javascript print 方法?

我有一个基于 jQuery 构建的名为 core.js 的文件 -

// API
var Screen = (function () { // private methods

    function print(text) {
        document.write(text);
    }

    return { // public methods
        print: function (text) {
            print(text);
        }
    };

}());

// MAIN
$(function () { // document ready

    Screen.print("Hello World.");

});

我还在 HTML 文档中设置了 qunit.js(和 .css)文件:

<!DOCTYPE html>
<html>
<head>
    <link href='qunit.css' rel='stylesheet' />
</head>
<body>
    <div id='qunit'></div>
    <script src='qunit.js'></script>
    <script src='core.js'></script>
</body>
</html>

我想我必须在导入 core-test.js 的 HTML 文档中添加一个脚本标记,其中包含我的单元测试。这就是我感到困惑的地方......

核心测试.js:

$(function () {
    test("print", function () {
        ok(Screen.print(text), "String is NOT null or undefined");
    });
});

【问题讨论】:

    标签: jquery qunit


    【解决方案1】:

    实际上这几乎是正确的。

    core-test.js 应该有某种类型的断言:

    test("print", function () {
      ok(Screen.print("") !== null, "String is not null");
      ok(Screen.print("") !== undefined, "String is defined");
    });
    

    HTML 文档应该导入 jQuery 以便识别 $ 标识符。

    <body>
        <div id='qunit'></div>
        <script src='jquery-version.js'></script>
        <script src='qunit.js'></script>
        <script src='core.js'></script>
        <script src='core-test.js'></script>
    </body>
    

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2012-11-23
      • 2017-01-01
      • 2015-11-21
      • 1970-01-01
      相关资源
      最近更新 更多