2012-08-27 18 views
1

如何使用qUnit測試javascript 打印方法?如何使用qUnit測試JavaScript打印方法?

我有一個名爲core.js建立在jQuery的文件 -

// 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."); 

}); 

我還設置了qunit.js(和.css)文件的HTML文檔中:

<!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"); 
    }); 
}); 

回答

0

事實上,這幾乎是正確的。

核心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>