我的项目结构是这样的:
├── src
| ├── js
| └── a.js
├── test
| ├── spec
| └── test_a.js
我的a.js
看起来像这样:
define('A/B/C', function(){
var D = ...
return D;
})
在我的karma.conf.js
中,我在requirejs
中添加了frameworks
,所需文件包含如下:
files: [
{pattern: 'src/js/*.js', included: false},
{pattern: 'test/spec/*.js', included: false},
'test/test-bootstrap.js'
]
我的test_a.js
看起来像:
define(['A/B/C'], function(D){
//test case
})
但浏览器会查找localhost:9876/base/A/B/C.js
并返回错误,因为浏览器中只存在localhost:9876/base/src/js/a.js
。
我的问题是,由于src/js/a.js
已经加载,我在requirejs
中引入karma.conf.js
,为什么不能通过其ID加载模块?我是否以错误的方式编写了A/B/C
模块?
require.config
中的我test/test-bootstrap.js
是:
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: 'http://localhost:9876/base',
paths: {
'jquery': 'test/lib/jquery-1.12.0.min'
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
我需要通过其id加载模块,因为我可能会更改我的项目结构,并且我不希望模块路径被硬编码到测试文件中。
答案 0 :(得分:1)
您必须配置RequireJS才能找到您的模块,因为您为其指定的名称与RequireJS可以使用的路径不对应。一般情况下,不建议在模块中使用硬代码名称,但如果确实有充分的理由,那么在调用angular.module('myApp', []);
var app = angular
.module('myApp', [
'ngRoute'
]);
app.config(function ($routeProvider) {
'use strict';
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'mainCntrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'aboutCntrl',
controllerAs: 'about'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('mainCntrl',['$scope', function ($scope) {
'use strict';
this.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
}]);
app.controller('aboutCntrl',['$scope', function ($scope) {
'use strict';
this.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
}]);
的{{1}}部分中,您应该有test/test-bootstrap.js
之类的代码这样:
require.config