Karma + AngularJS + requirejs:Karma无法运行我的角度模块和控制器。 当我尝试执行我的测试时,它给了我这个错误: "错误:[ng:areq] Argument' DemoController'不是一个功能,未定义"
我对angular和karma / requirejs测试框架很新。在网上发现了几个帖子,为我的角项目设置了业力+需求。所以我按照这里提到的步骤 http://karma-runner.github.io/0.8/plus/RequireJS.html http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/
我还提到了几个关于堆栈溢出的帖子,说可能是我错过了定义控制器的模块。但即便这样做,我也得到同样的错误。 我认为在karma.conf和/或test-main.js中我的js文件的配置或路径都缺少一些东西。
以下是我的项目结构的样子
以下是每个文件的外观
main.js - my require.js的主要文件
requirejs.config({
baseUrl : "/src",
paths : {
angular : '../lib/angular.min',
// Main Controller
main_ctrl : '/src/DemoController',
},
shim : {
angular : {
exports : "angular"
},
}
});
require([ 'angular', 'main_ctrl' ], function() {
'use strict';
console.log('Inside main.js');
angular.bootstrap(document, [ 'angularForKarma' ]);
});
app.js
define([ 'angular' ], function(angular) {
var app = angular.module('AngularForKarma', []);
return app;
});
DemoController.js
define([ 'angular', 'app'], function(angular, angularForKarma) {
angularForKarma.controller("DemoController", function($scope) {
$scope.compareValues = function(value1, value2) {
console.log('compareValues.................');
$scope.text = 'Hello World!';
if (angular.equals(value1, value2)) {
return true;
}
return false;
};
$scope.compareValues();
});
});
module.exports = function(config){ config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../..',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: [
'AngularForKarma/test/test-main.js',
{pattern: 'AngularForKarma/lib/*.js', included: false},
{pattern: 'AngularForKarma/src/**/*.js', included: false},
{pattern: 'AngularForKarma/test/**/*.js', included: false}
],
// list of files to exclude
exclude: [
'AngularForKarma/src/main.js'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultanous
concurrency: Infinity
})}
/
//Get a list of all the test files to include
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
// then do not normalize the paths
// var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
// allTestFiles.push(normalizedTestModule);
}
});
for (var file in window.__karma__.files) {
if (TEST_REGEXP.test(file)) {
allTestFiles.push(file);
}
}
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/AngularForKarma/src',
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start,
paths: {
angular: '../lib/angular',
angularMocks: '../lib/angular-mocks',
// Main Controller
main_ctrl : '../src/Controller/DemoController'
},
shim: {
angular: { exports: 'angular' },
angularMocks: { deps: ['angular'] }
}
});
以下是我的测试文件的外观
test.js
use strict';
define(['angular', 'app', 'angularMocks'], function (angular, app) {
describe('demoCtrl', function() {
var scope; // we'll use these in our tests
// mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('AngularForKarma'));
// mock the controller for the same reason and include $rootScope and $controller
beforeEach(angular.mock.inject(function($rootScope, $controller) {
console.log("inside beforeEach");
// create an empty scope
scope = $rootScope.$new();
// declare the controller and inject our empty scope
$controller('DemoController', {
$scope : scope
});
}));
// tests start here
it('should have variable text = "Hello World!"', function() {
console.log("inside first test");
expect(scope.text).toBe('Hello World!');
});
it('should compare values"', function() {
console.log("inside 2nd test");
expect(scope.compareValues('Anup', 'Anup')).toBe(true);
});
// it('should compare values"', function() {
// expect(scope.compareValues('Anup', 'Shreyas')).toBe(true);
// });
});
});
谁能告诉我在哪里弄错了?我现在被困在这3-4天了,但仍然无法弄清楚为什么它没有加载我的控制器和模块
由于