Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从intern.js测试中加载node.js http模块?_Javascript_Node.js_Unit Testing_Rest_Intern - Fatal编程技术网

Javascript 如何从intern.js测试中加载node.js http模块?

Javascript 如何从intern.js测试中加载node.js http模块?,javascript,node.js,unit-testing,rest,intern,Javascript,Node.js,Unit Testing,Rest,Intern,我正在尝试使用Intern测试框架来自动测试一个使用node.js和StrongLoop实现的简单REST API。StrongLoop提供了一个浏览器网页,我用它来验证RESTAPI是否已实现,我可以从Postman手动测试API 我尝试使用intern.js进行的第一个API测试检索集合中媒体对象的计数。HTTP方法是GET,URL是http://localhost:3000/api/media/count并且响应为{“count”:2} 当我运行intern.js测试时,我收到一个错误“错

我正在尝试使用Intern测试框架来自动测试一个使用node.js和StrongLoop实现的简单REST API。StrongLoop提供了一个浏览器网页,我用它来验证RESTAPI是否已实现,我可以从Postman手动测试API

我尝试使用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)\//
});
//了解有关在上配置此文件的详细信息。
//这些默认设置对大多数人都适用。以下*必须*更改的选项为
//软件包、套件、排除仪器和(如果需要功能测试)功能套件。
定义({
//检测代理将在其上侦听的端口
代理端口:9000,
//指向实习生代理的完全限定URL
proxyUrl:'http://localhost:9000/',
//所有环境的默认所需功能。任何
//在下面的“环境”数组中也指定了浏览器环境。请参阅
// https://code.google.com/p/selenium/wiki/DesiredCapabilities 用于标准Selenium功能和
// https://saucelabs.com/docs/additional-config#desired-酱汁实验室的功能。
//请注意,“build”功能将由Travis CI环境中的当前提交ID填充
//自动地
能力:{
“selenium版本”:“2.41.0”
},
//要对其运行集成测试的浏览器。请注意,如果与Sauce一起使用,则版本号必须是字符串
//将被置换的选项是browserName、version、platform和platformVersion;任何其他
//为环境指定的功能选项将按原样复制
环境:[
{browserName:'internet explorer',版本:'11',平台:'Windows 8.1'},
{browserName:'internet explorer',版本:'10',平台:'Windows 8'},
{browserName:'internet explorer',版本:'9',平台:'Windows 7'},
{browserName:'firefox',版本:'28',平台:['OSX10.9','Windows7','Linux']},
{browserName:'chrome',版本:'34',平台:['OSX10.9','Windows7','Linux']},
{browserName:'safari',版本:'6',平台:'OSX10.8'},
{browserName:'safari',版本:'7',平台:'OSX10.9'}
],
//应在远程WebDriver服务上执行的最大同时集成测试数
最大并发:3,
//用于WebDriver测试的隧道类的名称
隧道:“SaucelAbstrunnel”,
//运行单元测试时所需的AMD加载程序(client.html/client.js)。省略使用默认的Dojo
//装载机
useLoader:{
“主机节点”:“dojo/dojo”,
“主机浏览器”:“节点\模块/dojo/dojo.js”
},
//模块加载器的配置选项;指定AMD加载器支持的任何AMD配置选项
//可以在这里使用
加载器:{
//应在每个测试环境中向加载程序注册的包
套餐:[
{名称:'应用程序',位置:'.'.},
]
},
//要在每个浏览器中运行的非功能测试套件
套件:['tests/media'/*'myPackage/tests/foo','myPackage/tests/bar'*/],
//非功能测试完成后,在每个浏览器中运行的功能测试套件
功能套件:[/*'myPackage/tests/functional'*/],
//将URL与不应包含在代码覆盖率分析中的文件相匹配的正则表达式
不包括仪器:/^(?:测试|节点|模块)\//
});
我使用命令“c:\Repositories\app>intern client config=tests/intern”运行测试,并生成以下控制台输出:

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 |
---------------|-----------|-----------|-----------|-----------|
c:\Repositories\app>intern client config=tests/intern
失败:主-媒体-计数(0毫秒)
错误:尝试要求卸载模块http.request
在上下文中需要
按要求
at Test.registerSuite.count[作为测试]
在测试运行时
在
在signalListener
那就答应吧
运行测试时
在
在进程中。\u
1/1测试失败
1/1测试失败
---------------|-----------|-----------|-----------|-----------|
文件|%Stmts |%branchs |%Funcs |%line|
---------------|-----------|-----------|-----------|-----------|
测试\| 33.33 | 100 | 66.67 | 33.33|
media.js | 33.33 | 100 | 66.67 | 33.33|
---------------|-----------|-----------|-----------|-----------|
所有文件| 33.33 | 100 | 66.67 | 33.33|
---------------|-----------|-----------|-----------|-----------|
我的理解是http模块是一个核心模块,它是随node.js自动安装的,但是我想知道intern.js是否尽管基于节点,但不允许在没有额外配置的情况下访问核心模块。这是合理的,但是我已经看过intern.js教程和intern.js配置指南,但是没有找到一种方法来添加引用或依赖项,以便测试用例能够成功加载node.js http模块。我是intern.js的新手,所以我没有正确配置它或测试

我欢迎任何关于我做错了什么的意见/见解

致以最良好的祝愿


MW

Intern在AMD环境中运行其测试,因此
require
是AMD加载程序的
require
,而不是节点的,因此您会出错

要加载节点模块,请使用
intern/dojo/NodeAMD插件
define([
    ...,
    'intern/dojo/node!http'
], function (..., http) {
    // Now http contains the exports of Node's http module
});