1

qUnit を使用して JavaScript の印刷メソッドをテストするにはどうすればよいですか?

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 ドキュメントにスクリプト タグを追加する必要があると考えています。ここが迷うところです…

コア-test.js:

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

1 に答える 1

0

実際、それはほぼ正しいです。

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>
于 2012-08-27T07:57:07.697 に答える