2014-10-08 17 views
2

我正在嘗試使用實驗室測試框架來自動化測試使用node.js和StrongLoop實現的簡單REST API。 StrongLoop提供了一個我用來驗證REST API已經實現的瀏覽器網頁,我可以從Postman手動測試API。如何從intern.js測試中加載node.js http模塊?

我嘗試使用intern.js的第一個API測試檢索集合中媒體對象的計數。 HTTP方法是GET,URL是http://localhost:3000/api/media/count,響應是{「count」:2}。

當我運行我的intern.js測試時收到錯誤「錯誤:嘗試要求卸載的模塊http」。

我在我的node.js應用程序中有一個\ tests子目錄。我的測試用例是media.js:

define([ 
    'intern!object', 
    'intern/chai!assert' 
], function (registerSuite, assert, media) { 
    registerSuite({ 
     name: 'media', 
     count: function() { 
     var http = require("http"); 
     var request = http.request; 
     request({ 
      url: "http://localhost:3000/api/media/count", 
      method: "GET" 
     }, function (error, response, body) { 
      console.log("Status", response.statusCode); 
      console.log("Headers", JSON.stringify(response.headers)); 
      console.log("Response received", body); 
     }); 

     assert.strictEqual(body, '{"count": 2}', 
      url + ' should return 2 items'); 
     } 
    }); 
}); 

我intern.js配置文件是:

// Learn more about configuring this file at <https://github.com/theintern/intern/wiki/Configuring-Intern>. 
// These default settings work OK for most people. The options that *must* be changed below are the 
// packages, suites, excludeInstrumentation, and (if you want functional tests) functionalSuites. 
define({ 
    // The port on which the instrumenting proxy will listen 
    proxyPort: 9000, 

    // A fully qualified URL to the Intern proxy 
    proxyUrl: 'http://localhost:9000/', 

    // Default desired capabilities for all environments. Individual capabilities can be overridden by any of the 
    // specified browser environments in the `environments` array below as well. See 
    // https://code.google.com/p/selenium/wiki/DesiredCapabilities for standard Selenium capabilities and 
    // https://saucelabs.com/docs/additional-config#desired-capabilities for Sauce Labs capabilities. 
    // Note that the `build` capability will be filled in with the current commit ID from the Travis CI environment 
    // automatically 
    capabilities: { 
     'selenium-version': '2.41.0' 
    }, 

    // Browsers to run integration testing against. Note that version numbers must be strings if used with Sauce 
    // OnDemand. Options that will be permutated are browserName, version, platform, and platformVersion; any other 
    // capabilities options specified for an environment will be copied as-is 
    environments: [ 
     { browserName: 'internet explorer', version: '11', platform: 'Windows 8.1' }, 
     { browserName: 'internet explorer', version: '10', platform: 'Windows 8' }, 
     { browserName: 'internet explorer', version: '9', platform: 'Windows 7' }, 
     { browserName: 'firefox', version: '28', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] }, 
     { browserName: 'chrome', version: '34', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] }, 
     { browserName: 'safari', version: '6', platform: 'OS X 10.8' }, 
     { browserName: 'safari', version: '7', platform: 'OS X 10.9' } 
    ], 

    // Maximum number of simultaneous integration tests that should be executed on the remote WebDriver service 
    maxConcurrency: 3, 

    // Name of the tunnel class to use for WebDriver tests 
    tunnel: 'SauceLabsTunnel', 

    // The desired AMD loader to use when running unit tests (client.html/client.js). Omit to use the default Dojo 
    // loader 
    useLoader: { 
     'host-node': 'dojo/dojo', 
     'host-browser': 'node_modules/dojo/dojo.js' 
    }, 

    // Configuration options for the module loader; any AMD configuration options supported by the specified AMD loader 
    // can be used here 
    loader: { 
     // Packages that should be registered with the loader in each testing environment 
     packages: [ 
     { name: 'app', location: '.' }, 
     ] 
    }, 

    // Non-functional test suite(s) to run in each browser 
    suites: [ 'tests/media' /* 'myPackage/tests/foo', 'myPackage/tests/bar' */ ], 

    // Functional test suite(s) to run in each browser once non-functional tests are completed 
    functionalSuites: [ /* 'myPackage/tests/functional' */ ], 

    // A regular expression matching URLs to files that should not be included in code coverage analysis 
    excludeInstrumentation: /^(?:tests|node_modules)\// 
}); 

我使用命令`C運行測試:\庫\應用>實習生-客戶端配置=測試/實習生」和下面控制檯輸出產生:

c:\Repositories\app>intern-client config=tests/intern 
FAIL: main - media - count (0ms) 
Error: Attempt to require unloaded module http.request 
    at contextRequire <D:\Users\username\AppData\Roaming\npm\node_modules\inte 
rn\node_modules\dojo\dojo.js:255:12> 
    at req <D:\Users\username\AppData\Roaming\npm\node_modules\intern\node_mod 
ules\dojo\dojo.js:30:10> 
    at Test.registerSuite.count [as test] <tests\media.js:23:27> 
    at Test.run <D:\Users\username\AppData\Roaming\npm\node_modules\intern\lib 
\Test.js:169:19> 
    at <D:\Users\username\AppData\Roaming\npm\node_modules\intern\lib\Suite.js: 
237:13> 
    at signalListener <D:\Users\username\AppData\Roaming\npm\node_modules\inte 
rn\node_modules\dojo\Deferred.js:37:21> 
    at Promise.then.promise.then <D:\Users\username\AppData\Roaming\npm\node_m 
odules\intern\node_modules\dojo\Deferred.js:258:5> 
    at runTest <D:\Users\username\AppData\Roaming\npm\node_modules\intern\lib\ 
Suite.js:236:46> 
    at <D:\Users\username\AppData\Roaming\npm\node_modules\intern\lib\Suite.js: 
249:7> 
    at process._tickCallback <node.js:419:13> 
1/1 tests failed 
1/1 tests failed 

---------------|-----------|-----------|-----------|-----------| 
File   | % Stmts |% Branches | % Funcs | % Lines | 
---------------|-----------|-----------|-----------|-----------| 
    tests\  |  33.33 |  100 |  66.67 |  33.33 | 
     media.js |  33.33 |  100 |  66.67 |  33.33 | 
---------------|-----------|-----------|-----------|-----------| 
All files  |  33.33 |  100 |  66.67 |  33.33 | 
---------------|-----------|-----------|-----------|-----------| 

我理解的是在HTTP模塊與node.js的自動安裝的芯模塊,但是不知intern.js,雖然基於節點,不p無需額外配置就可以訪問核心模塊。這個合理的,但我已經看過intern.js教程和intern.js配置指南,但還沒有找到一種方法來添加引用或依賴項,使測試案例能夠成功加載node.js http模塊。我是intern.js的新手,所以希望我沒有正確配置它或測試。

我歡迎任何意見/見解我做錯了什麼。

最好的問候,

兆瓦

+0

一個額外的說明:對於這個第一步,我試圖測試沒有任何UI客戶端組件的REST API,所以我正在尋找一個不依賴於瀏覽器或瀏覽器仿真的解決方案。這就是爲什麼我想使用node.js HTTP模塊。 – mw1967 2014-10-08 21:55:04

回答

9

實習生運行其測試在AMD的環境,所以require是AMD裝載機require,沒有節點的,因此你的錯誤。

要加載節點模塊,使用AMD intern/dojo/node!插件,它們包括在模塊的依賴關係,例如:

define([ 
    ..., 
    'intern/dojo/node!http' 
], function (..., http) { 
    // Now http contains the exports of Node's http module 
}); 

這是實習生的用戶指南中Testing CommonJS Modules下記錄。

+1

可能要注意的是:這不僅適用於標準節點模塊,而且可以使用「http」,但也適用於任何npm模塊。 – Kiechlus 2016-03-15 13:39:26