这是我的目录层次结构:
这是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,需求的配置文件和业力的起点:
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
});
我的一个spec文件是:
define(['underscore', 'jquery', 'angular', 'angularRoute', 'angularMocks'], function(_) {
describe('UnitTest: ParamsSettings', function(_) {
it('is defined', function() {
expect(_.size([1,2,3])).toEqual(3);
})
})
});
我无法加载spec文件中的任何依赖项。非常感谢您的回答。
编辑:
karma.conf.js中的 main.spec.js 可以使用url http://localhost:9876/base/test/main.spec.js
找到,但我有一个名为 another.spec.js ,它位于前一个文件的同一目录中, {<1}}无法找到 。
添加了@glepretre
提供的内容答案 0 :(得分:3)
问题解决了。 实际上,Karma服务器没有提供任何我的lib文件。我们应该通过设置 karma.conf.js 的正确文件选项来指定Karma服务器所服务的文件的位置。
所以我编辑如下:
...,
files: [
{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: 'vendor/jquery/dist/jquery.min.js', included: false},
{pattern: 'vendor/underscore/underscore.js', included: false},
{pattern: 'backend/public/**/*.js', included: false},
{pattern: 'test/*.spec.js', included: false},
{pattern: 'test/**/*.spec.js', included: false},
{pattern: 'test/main.spec.js', included: false},
'test/test-main-cfg.js'
],
...
实际上它可以更简单:
...,
files: [
{pattern: 'vendor/**/*.js', included: false},
{pattern: 'backend/public/**/*.js', included: false},
{pattern: 'test/*.spec.js', included: false},
{pattern: 'test/**/*.spec.js', included: false},
{pattern: 'test/main.spec.js', included: false},
'test/test-main-cfg.js'
],
...
这是文件选项的官方文档:Here
答案 1 :(得分:2)
你不应该添加一些垫片吗?
在您的require配置中:
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
});
在您的spec文件中:
// moved underscore first to get the module in the function args
define(['underscore', 'jquery', 'angular', 'angularRoute', 'angularMocks'],
function(_) {
describe('UnitTest: ParamsSettings', function() {
it('is defined', function() {
expect(_.size([1,2,3])).toEqual(3);
});
});
});
编辑:顺便说一句,如果你想使用requireJS查看角项目的入门应用程序结构,我们已经与我的团队一起开发了。它基于official Angular best practices for App structure,您可以在此处获取:angular-requirejs-ready。