【发布时间】:2016-05-06 14:41:57
【问题描述】:
我在将测试文件从另一个目录加载到我的单元测试文件时遇到问题。我正在使用 requirejs 和 karma 另一个问题是在测试场景文件项目中加载 chai-http 模块的正确方法
|--service
|--abcfile.js // i want to use method implemented in this file and
test it.
|---node_modules
-- all node library like karma,chai module
|--test-main
|--test-main.js
| karma.conf.js
Karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'requirejs'],
files: [
{pattern: 'node_modules/**/*.js', included: false},
{ pattern: 'node_modules/**/*/*.js', included: false },
'test/test-main/test-main.js',
{ pattern: 'test/test-main/*.js', included: false }
],
exclude: [
],
reporters: ['progress','html'],
htmlReporter: {
outputFile: 'test/report/units.html',
pageTitle: 'Tests',
subPageTitle: 'A sample project description'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
captureTimeout: 60000,
singleRun: false
});
};
测试主
(function() {
var specFiles = null;
var baseUrl = '';
var requirejsCallback = null;
if (typeof window != 'undefined' && window.__karma__ != undefined) {
baseUrl = '/base';
requirejsCallback = window.__karma__.start;
specFiles = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/.*\/javascript\/*\/.+_Test\.js$/.test(file)) {
specFiles.push(file);
}
}
}
}
requirejs.config({
baseUrl: baseUrl,
paths: {
'chai': './node_modules/chai/chai',
'sinon': './node_modules/sinon/pkg/sinon',
'chaiHttp': './node_modules/chai-http/dist/chai-http',
},
deps: specFiles,
callback: requirejsCallback
});
})();
用户测试场景文件
define(function (chai, sinon, chaiHttp) {
var expect = chai.expect;
var service= require('/service/abcfile.js');// this is file where i want one of the function to test
chai.use(chaiHttp);
错误 加载资源失败:服务器响应状态为 404(未找到)
【问题讨论】:
-
@Louis 我已编辑。/service/abcfile.js 不在 node_module 下。它在根目录下。如果我放置模块,那么我必须包含在 test-main 文件中才能像路径一样添加该模块:{ 'service': '/service/abcfile', 'chai': './node_modules/chai/chai', 'sinon ': './node_modules/sinon/pkg/sinon', 'chaiHttp': './node_modules/chai-http/dist/chai-http', },
标签: requirejs mocha.js karma-runner chai