Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Node.js 运行所有';test.html';目录中的文件-mocha phantomjs_Node.js_Unit Testing_Phantomjs_Mocha.js_Mocha Phantomjs - Fatal编程技术网

Node.js 运行所有';test.html';目录中的文件-mocha phantomjs

Node.js 运行所有';test.html';目录中的文件-mocha phantomjs,node.js,unit-testing,phantomjs,mocha.js,mocha-phantomjs,Node.js,Unit Testing,Phantomjs,Mocha.js,Mocha Phantomjs,我有一个模块,我正在使用mocha phantomjs测试这个模块。我创建了package.json文件 { "name" : "demo-test", "scripts": { "test": "npm run test-debug", "test-debug": "mocha-phantomjs ./test/Test1.html" }, "dependencies" : { "mocha" : "1.13.x", "co

我有一个模块,我正在使用mocha phantomjs测试这个模块。我创建了
package.json
文件

{
  "name"        : "demo-test",
  "scripts": {
    "test": "npm run test-debug",
    "test-debug": "mocha-phantomjs ./test/Test1.html"
  },
  "dependencies" : {
    "mocha"     : "1.13.x",
    "commander" : "1.2.x",
    "which"     : "~1.0.5",
    "mocha-phantomjs": "3.3.2"
  },
  "devDependencies" : {
    "chai"          : "1.8.x",
    "coffee-script" : "1.6.x",
    "requirejs"     : "2.1.x",
    "jquery"        : "2.1.0"
  }
}
然后我运行
npm安装
,然后运行
npm测试
。它工作正常,可以运行
test1.html
的测试。 现在我希望在运行
npmtest
时执行test目录下的所有文件(test1,test2,…)


我可以在package.json文件中单独调用然后运行所有html文件,但如果有办法加载所有html文件

通常,您会将一个Tests.html文件传递给您的mocha phantomjs运行程序,该运行程序将使用脚本标记加载所有要运行的测试文件

Tests.html将包含:

<script src="controller-tests/one-controller-test.js"></script>
<script src="controller-tests/another-controller-test.js"></script>
<script src="controller-tests/yet-another-controller-test.js"></script>
<script src="service-tests/one-service-test.js"></script>
<script src="service-tests/another-service-test.js"></script>
<script src="service-tests/yet-another-service-test.js"></script>
控制器测试/init.js

require('controller-tests/init.js');
require('service-tests/init.js');
require('one-controller-test.js');
require('another-controller-test.js');
require('yet-another-controller-test.js');
require('one-service-test.js');
require('another-service-test.js');
require('yet-another-service-test.js');
服务测试/init.js

require('controller-tests/init.js');
require('service-tests/init.js');
require('one-controller-test.js');
require('another-controller-test.js');
require('yet-another-controller-test.js');
require('one-service-test.js');
require('another-service-test.js');
require('yet-another-service-test.js');
我同意达纳里的观点

我使用相同的引导加载程序和测试设置

摩卡BDD

据推测,默认情况下,在“BDD”模式下运行Mocha会在项目根目录中的“test”目录中查找html文件。它首先查找名为“test.html”的文件。“test/test.html”是Mocha用作夹具文件的默认路径目标

关于RequireJS/AMD的问题

关于客户端(AMD)风格的RequireJS,我使用了二进制分支模式

-index.html
  |
  -/js/rjsMain.js
-test/test.html
  |
  -/js/rjsTest.js
我将我的RequireJS配置部分分离到一个新文件中(我称我的文件为“rjsConfig.js”)。该文件包含路径别名、依赖项垫片等

因此,我的RequireJS“data main”文件“/js/rjsMain.js”如下所示:

require(['rjsConfig'], function () {
    require(['app/main'], function () {});
});
然后我复制了Require“main.js”文件,为我的Mocha fixture文件添加了一个备用入口点

这样做的原因是,您不必根据需要维护两个单独的配置文件。有一个共享文件很好

PhantomJS

让PhantomJS实际加载并与异步加载的内容交互?那是另一回事。这可能相当棘手

幸运的是,我找到了一个可以实现这一功能的文件

它没有一个很好的名字,但是——至少对我来说——“load_ajax.js”是让它工作所需要的秘方


您需要对其进行自定义,以与您的本地路线相对应。

此[链接][1]可以帮助您。同时检查[此][2]链接。[1]: [2]: