2013-05-09 20 views
6

我想測試實習生,看看它是否適合測試框架。我正在嘗試在Intern中測試以下代碼。不能讓實習生運行Node.js模塊

var HelloWorld; 

HelloWorld = (function() { 

    function HelloWorld (name) { 
    this.name = name || "N/A"; 
    } 

    HelloWorld.prototype.printHello = function() { 
    console.log('Hello, ' + this.name); 
    }; 

    HelloWorld.prototype.changeName = function(name) { 
    if (name === null || name === undefined) { 
     throw new Error('Name is required'); 
    } 
    this.name = name; 
    }; 

    return HelloWorld; 

})(); 

exports = module.exports = HelloWorld; 

該文件位於 'JS-測試項目/節點/ LIB/HelloWorld.js' 和實習生位於 'JS-測試項目/實習生'。我正在使用Intern的1.0.0分支。每當我試圖包含文件並運行測試時,我都沒有在「默認爲控制檯記者」之後得到任何輸出。這是測試文件。

define([ 
    'intern!tdd', 
    'intern/chai!assert', 
    'dojo/node!../lib/HelloWorld' 
], function (tdd, assert, HelloWorld) { 
    console.log(HelloWorld); 
}); 
+1

對於有人不熟悉的節點。 js,這聽起來像是一個實習職位:) – 2013-05-09 20:23:25

+1

我討厭當我無法讓我的實習生運行我想要的東西。 – AaronLS 2013-05-09 20:47:29

回答

7

1.假設以下目錄結構(基於問題):

js-test-projects/ 
    node/ 
     lib/ 
      HelloWorld.js - `HelloWorld` Node module 
     tests/ 
      HelloWorld.js - Tests for `HelloWorld` 
      intern.js  - Intern configuration file 
    intern/ 

2.您實習生配置文件應包含在node包以及任何套房運行信息:

// ... 

// Configuration options for the module loader 
loader: { 
    // Packages that should be registered with the loader in each testing environment 
    packages: [ 'node' ] 
}, 

// Non-functional test suite(s) to run 
suites: [ 'node/tests/HelloWorld' ] 

// ... 

3.你們的測試文件■使用實習生的版本道場的HOULD負荷HelloWorld,像這樣:

define([ 
    'intern!tdd', 
    'intern/chai!assert', 
    'intern/dojo/node!./node/lib/HelloWorld.js' 
], function (tdd, assert, HelloWorld) { 
    console.log(HelloWorld); 
}); 

注:你不使用實習生的版本的Dojo加載HelloWorld節點模塊在這個AMD測試中,它是隻是一個方便的方法。如果你有其他AMD插件那個節點需要一個節點模塊,那就太好了。

4.最後,運行在Node.js的環境測試,使用實習生的client.js節點亞軍由來自intern目錄中發出以下命令:

node client.js config=node/tests/intern 
+2

請注意,如果您的Node模塊已經在可解析的'node_modules'目錄中,那麼您只需使用'intern/dojo/node!node/lib/HelloWorld'。 – 2013-05-15 19:40:28

相關問題