2013-05-09 24 views
6

Tôi đang cố gắng kiểm tra Intern để xem liệu nó có phù hợp với một khuôn khổ thử nghiệm hay không. Tôi đang cố gắng kiểm tra mã sau trong Intern.Không thể lấy Intern để chạy Mô-đun 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; 

Tệp nằm trong 'js-test-projects/node/lib/HelloWorld.js' và Intern được đặt tại 'js-test-projects/intern'. Tôi đang sử dụng chi nhánh Intern của 1.0.0. Bất cứ khi nào tôi cố gắng bao gồm các tập tin và chạy thử nghiệm tôi không nhận được bất kỳ đầu ra sau khi "Defaulting to console reporter". Đây là tệp thử nghiệm.

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

Đối với một người không quen thuộc với nút. js, điều này nghe giống như một bài thực tập :) –

+1

Tôi ghét khi tôi không thể có được thực tập sinh của tôi để điều tôi muốn. – AaronLS

Trả lời

7

1. Giả sử cấu trúc thư mục sau (dựa trên các câu hỏi):

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

2. tập tin cấu hình Intern của bạn nên chứa thông tin về các node gói và bất cứ dãy phòng để chạy:

// ... 

// 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. Tệp thử nghiệm của bạn hould tải HelloWorld sử dụng phiên bản của Dojo Intern của, như thế này:

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

Lưu ý: Bạn không sử dụng phiên bản của Dojo Intern để nạp module HelloWorld nút trong thử nghiệm AMD này, nó là chỉ là một cách thuận tiện để làm như vậy. Nếu bạn có một số plugin AMD khác mà nút-đòi hỏi một mô-đun nút, đó là hoàn toàn tốt đẹp.

4. Cuối cùng, để chạy thử nghiệm trong một môi trường Node.js, sử dụng Á hậu client.js nút Intern bằng cách ban hành lệnh sau từ bên trong thư mục intern:

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

Lưu ý rằng nếu module Node của bạn đã nằm trong thư mục 'node_modules' có thể phân giải thì bạn chỉ cần sử dụng' intern/dojo/node! Node/lib/HelloWorld'. –

Các vấn đề liên quan