Javascript 调用karma测试时未定义角度错误
我确信这与angular-mock.js有关,但我不知道我需要做什么,因为一切似乎都很好。我只是在使用angular seed应用程序的默认设置。 请帮忙解决这个问题 karma.conf.jsJavascript 调用karma测试时未定义角度错误,javascript,angularjs,karma-jasmine,Javascript,Angularjs,Karma Jasmine,我确信这与angular-mock.js有关,但我不知道我需要做什么,因为一切似乎都很好。我只是在使用angular seed应用程序的默认设置。 请帮忙解决这个问题 karma.conf.js module.exports = function(config){ config.set({ basePath : '../', files : [ 'bower_components/angular/angular.js', 'bower_compon
module.exports = function(config){
config.set({
basePath : '../',
files : [
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js'
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
});
};
'use strict';
/* Controllers */
var app = angular.module('myApp.controllers', []);
app.constant('RESTURL', 'http://'+ location.hostname + ':8003');
app.controller('MainCtrl', ['$scope', 'dataService', 'flash', 'mySharedService','facetSearchService', 'facetSelectionService', 'RESTURL', function($scope, dataService, flash, sharedService, facetSearch, facet, RESTURL) {
$scope.RESTURL = RESTURL;
$scope.loading = true;
$scope.data = null;
$scope.resultCount = 0;
$scope.currentPage = 0;
$scope.pageSize = 10;
....
])}
'use strict';
/* jasmine specs for controllers go here */
describe('controllers', function(){
beforeEach(module('myApp.controllers'));
it('should test MainCtrl', inject(function($controller) {
//spec body
var scope = {},
ctrl = $controller('MainCtrl', {$scope:scope});
expect($scope.RESTURL).toBe('http://'+ location.hostname + ':8003'));
}));
});
controllers.js
module.exports = function(config){
config.set({
basePath : '../',
files : [
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js'
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
});
};
'use strict';
/* Controllers */
var app = angular.module('myApp.controllers', []);
app.constant('RESTURL', 'http://'+ location.hostname + ':8003');
app.controller('MainCtrl', ['$scope', 'dataService', 'flash', 'mySharedService','facetSearchService', 'facetSelectionService', 'RESTURL', function($scope, dataService, flash, sharedService, facetSearch, facet, RESTURL) {
$scope.RESTURL = RESTURL;
$scope.loading = true;
$scope.data = null;
$scope.resultCount = 0;
$scope.currentPage = 0;
$scope.pageSize = 10;
....
])}
'use strict';
/* jasmine specs for controllers go here */
describe('controllers', function(){
beforeEach(module('myApp.controllers'));
it('should test MainCtrl', inject(function($controller) {
//spec body
var scope = {},
ctrl = $controller('MainCtrl', {$scope:scope});
expect($scope.RESTURL).toBe('http://'+ location.hostname + ':8003'));
}));
});
controllerSpec.js
module.exports = function(config){
config.set({
basePath : '../',
files : [
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js'
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
});
};
'use strict';
/* Controllers */
var app = angular.module('myApp.controllers', []);
app.constant('RESTURL', 'http://'+ location.hostname + ':8003');
app.controller('MainCtrl', ['$scope', 'dataService', 'flash', 'mySharedService','facetSearchService', 'facetSelectionService', 'RESTURL', function($scope, dataService, flash, sharedService, facetSearch, facet, RESTURL) {
$scope.RESTURL = RESTURL;
$scope.loading = true;
$scope.data = null;
$scope.resultCount = 0;
$scope.currentPage = 0;
$scope.pageSize = 10;
....
])}
'use strict';
/* jasmine specs for controllers go here */
describe('controllers', function(){
beforeEach(module('myApp.controllers'));
it('should test MainCtrl', inject(function($controller) {
//spec body
var scope = {},
ctrl = $controller('MainCtrl', {$scope:scope});
expect($scope.RESTURL).toBe('http://'+ location.hostname + ':8003'));
}));
});
项目文件结构:
我相信您运行了“bower安装”来安装依赖项
bower_组件的路径不正确。“./”的基本路径将使karma显示在项目的根目录中,但您的bower_组件位于“app”文件夹中。karma.conf中的“文件”需要加上前缀“app”。是的,我已经安装了所有依赖项&我看到文件
angular mocks.js
也可能是您的基本路径。除非你更改了周围的一些文件夹,否则它应该是空的。我没有对项目结构做任何更改,因为这是我的第一个角度项目。我不想做我不熟悉的愚蠢的更改。是的。这就是问题所在。很抱歉回来这么晚。谢谢你,杰德维特!