这是我的目录层次结构:
- 应用程序
- 后端/
- 前端/
- 控制器/
- ...
- 测试/
- 控制器/
- ...
- 测试主cfg.js
- 小贩/
- 角/角.min.js
- 下划线/下划线.js
- ...
- 业力.conf.js
这是 karma.conf.js 的内容:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'requirejs'],
files: [
{pattern: 'test/**/*.spec.js', included: false},
{pattern: 'vendor/angular/angular.min.js', included: false},
{pattern: 'vendor/angular/angular-route.min.js', included: false},
{pattern: 'vendor/angular/angular-mocks.js', included: false},
{pattern: 'backend/public/**/*.js', included: false},
{pattern: 'test/main.spec.js', included: false},
'test/test-main-cfg.js'
],
exclude: [
'**/*.swp'
],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome', 'Firefox', 'Safari'],
captureTimeout: 60000,
singleRun: false
});
};
下面是test/test-main-cfg.js,requires的配置文件和karma的起点:
var tests = [],
file;
for (file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/.spec\.js$/.test(file)) {
tests.push(file);
}
}
}
require.config({
baseUrl: '/base/',
paths: {
'jquery': "vendor/jquery/dist/jquery.min",
'angular': 'vendor/angular/angular.min',
'angularRoute': 'vendor/anguar-route.min',
'angularMocks': 'vendor/angular/angular-mocks',
'underscore': 'vendor/underscore/underscore'
},
shim: {
'underscore': {
exports: '_'
},
// added
'jquery': {
exports: 'jquery'
},
'angular': {
deps: ['jquery'],
exports: 'angular'
},
'angularRoute': {
deps: ['angular'],
exports: 'angularRoute'
},
'angularMocks': {
deps:['angular'],
exports: 'angularMocks'
}
},
deps: tests,
callback: window.__karma__.start
});
我的规范文件之一是:
define(['underscore', 'jquery', 'angular', 'angularRoute', 'angularMocks'], function(_) {
describe('UnitTest: ParamsSettings', function(_) {
it('is defined', function() {
expect(_.size([1,2,3])).toEqual(3);
})
})
});
我无法在规范文件中加载任何依赖项。非常感谢您的回答。
编辑:
karma.conf.js中的main.spec.js 可以使用 url 找到http://localhost:9876/base/test/main.spec.js
,但是我有一个名为another.spec.js的文件,它与前一个文件在同一目录下,无法通过 url 找到http://localhost:9876/base/test/another.spec.js
。
添加了@glepretre 提供的内容