Controller test always giving undefined $scope - angularjs

I am just trying to get started unit testing Angular code and am trying to use Karma and Jasmine for testing. I have checked through several different tutorials and have emulated their code, but every time, the $scope is undefined in my test. I have been working all day on this, have read every related Stackoverflow question (a couple seemed related but did not get answered), keep making things simpler and simpler to narrow down the problem, etc. I am about to pull my hair out. If anyone can help me, I would appreciate it greatly!
karma.config.js:
// Karma configuration
// Generated on Tue Aug 11 2015 11:40:35 GMT-0500 (Central Daylight Time)
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'],
// list of files / patterns to load in the browser
files: [
'Scripts/angular-min.js',
'Scripts/angular-mocks.js',
'Scripts/angular-route-min.js',
'Scripts/angular-resource.min.js',
'Scripts/hr-module.js',
'Scripts/jquery-1.8.2.js',
'Scripts/Employee/*.js'
],
// list of files to exclude
exclude: [
'Scripts/_references.js',
'Scripts/jquery-1.8.2.intellisense.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_INFO,
// 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
})
}
simple-controller.js:
(function () {
'use strict';
angular.module('hrModule').controller('SimpleController', ['$scope', function ($scope) {
$scope.greeting = "hello world";
}]);
}());
Test for simple-controller:
'use strict';
describe('SimpleController', function () {
beforeEach(angular.mock.module('hrModule'));
var $controller,
scope,
SimpleController;
beforeEach(angular.mock.inject(function ($rootScope, _$controller_) {
scope = $rootScope.$new();
$controller = _$controller_;
SimpleController = $controller('SimpleController', { $scope: scope });
}));
it('says hello world', function () {
expect(scope).toBeDefined();
});
});
I keep getting an ($injector:modulerr) error when I run it, and says "Expected undefined to be defined". If anyone can see any issues here, please let me know! Thank you!

Thank you, PSL for working with me on this. I really appreciate you coming back to keep offering tips.
However, the solution was much more mundane than that. It was an incompatibility between the angular.js file that I had grabbed from Nuget (the most recent release, 1.4.x) and the angular-mock.js script I had downloaded from a tutorial link (1.3.3). For some reason this caused modulerr error that had me baffled, but I figured it out while rebuilding a smaller test project with the same files to test it in the browser.
So if anyone else is getting an error like this, open your angular library files and ensure that they all share the same version number!!

Related

how to get the phantomjs working for karma testrunner?

I am trying to run the karmatest runner with phantomJS in windows 7. This is the config file:
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-06-01 using
// generator-karma 0.8.3
module.exports = function (config) {
'use strict';
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
//preprocessors: {
// 'app/**/*.js': ['coverage']
//},
//
preprocessors: {
'**/*.html': ['ng-html2js'],
'app/**/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.js',
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-ui-router/release/angular-ui-router.js',
'bower_components/angular-bootstrap/ui-bootstrap.js',
'bower_components/angular-smart-table/dist/smart-table.min.js',
'bower_components/underscore/underscore.js',
'app/modules/**/*.js',
'app/modules/app.js',
'app/modules/config.js',
//'../app/scripts_old/**/*.js',
// 'test/mock/**/*.js',
'test/spec/**/*.spec.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'PhantomJS'
],
// Which plugins to enable
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-coverage',
'karma-chrome-launcher'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DISABLE
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
I installed the karmaphantomjs launcher here:
https://www.npmjs.com/package/karma-phantomjs-launcher
I also set the phantomjs_bin environment variable to \phantomjs.exe
When I use the chromebrowser it works fine. How can I get it running for phantomJS?
Use the following process:
Remove the PHANTOMJS_BIN environment variable
Find the path to phantomjs via the cmd shell, then run:
where phantomjs
Copy the location to the the PATH environment variable by running path, then appending it to the list
Because you can override the phantomjs_bin path using env, that meant if you did that it would try to run the phantomjs script thru phantomjs.exe, not node.exe. This was fixable but led to more codepaths and I prefered to par things back to a simpler solution.
References
karma phantomjs-launcher fix: Switched from launching phantomjs via node to ensuring that the .exe is always launched
karma phantomjs-launcher source: index.js

karma.conf.js automatic file ordering?

I have a large angularjs project ordered by features. I'd like to setup unit testing but I'm having trouble getting the karma.conf.js file ordering setup.
I tried specifying a simple glob pattern like **/*.js but many of my modules failed to load due to the ordering that they're included in Karma when ran. As I understand, it's alphabetical, first match.
I was able to resolve this by manually figuring out the ordering by doing something like this:
// list of files / patterns to load in the browser
files: [
// External scripts
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
// NOTE: ORDER IS IMPORTANT
// Modules: load module first, then all controllers, services, etc
'scripts/module1/module1.js',
'scripts/module1/**/*.js',
'scripts/module2/module2.js',
'scripts/module2/**/*.js',
// Load overall app module last
'scripts/app.js',
// Load mocks and tests
'test/mock/**/*.js',
'test/spec/**/*.js'
],
This seems like it will be cumbersome to maintain over time as new modules are added. Is there a way to automatically resolve the ordering?
Note: One possible solution I was thinking was to concat all the files together but I googled to see if others are doing this and found no examples.
I think you may look into different solutions here, depending on how you want to manage your project.
Solution n.1
Use AMD/RequireJS: do not load your modules all at once but just require them when you need.
Sometimes it may not fit your needs tough making the project over complicated, so look below.
Note: this is IMHO the most elegant solution and karma does support requireJS.
Solution n.2
Create a namespace where you append all your stuff and start them when the DOM is ready (usually pretty quick, but it really depends on how much scripts you load)
// module A
// Something like this should be fine
(function(){
window.MyNamespace = window.MyNamespace || {};
// append things to the namespace...
})();
// module B
(function(){
window.MyNamespace = window.MyNamespace || {};
// append things to the namespace...
})();
// This is very rough but it should give the idea
window.ondomready = MyNamespace.start;
Note: while this may work you have to fiddle a bit with your project to change the structure accordingly.
I would go for the one above unless you really hates requireJS and all the modules stuff.
Solution n.3
Programmatically order your Karma files: I wrote about it in this answer here.
Note: this is the less mantainable of the options.
If this
// Modules: load module first, then all controllers, services, etc
'scripts/module1/module1.js',
'scripts/module1/**/*.js',
'scripts/module2/module2.js',
'scripts/module2/**/*.js',
could instead force the same filenaming convention on the module-declaration file, to be this
// Modules: load module first, then all controllers, services, etc
'scripts/module1/moduleDeclaration.js',
'scripts/module1/**/*.js',
'scripts/module2/moduleDeclaration.js',
'scripts/module2/**/*.js',
Then you could do this
// Modules: load module first, then all controllers, services, etc
'scripts/**/moduleDeclaration.js',
'scripts/**/*.js',
I do something similar, though I do break out interfaces & types between moduleDeclaration and "the rest of it".
I use browserify and i have no prblems with it, here is my configuration:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['browserify', 'jasmine'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'dev/**/*Spec.js'
],
exclude: [
],
browserify: {
watch: false,
debug: true
},
preprocessors: {
'dev/**/*Spec.js': ['browserify']
},
reporters: ['progress'],
port: ****,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: true
});
};

karma/jasmine angular tests fails on osx

I have a big angular app whose tests have been running fine through grunt-karma/karma-jasmine since the beginning of the project. Recently, tests began to fail most of the time and I can't figure out what is going wrong.
I have a git commit that works every times, and the next one fails most of the times, and the next ones two. I've been fiddling with it for hours without being able to isolate anything that would make the tests pass on a consistent basis. Every time I think I have found what was tripping up the test suite, trying to use that knowledge a few commits later ends up in error anyway.
The first 25 tests always pass ok, and then I get an error message that doesn't bring much to the table :
Error: [$injector:modulerr] Failed to instantiate module app-module-common due to:
Error: [$injector:unpr] Unknown provider:
http://errors.angularjs.org/1.2.26/$injector/unpr?p0=
at /Users/hudson/workspace/app-recast-master/build/js/bottom/vendor/dev/20-angular.js:3802
The weird thing in this message is that no provider is specified as being unknown.
This is happening on the osx based box that is in charge of building the site, but not on my windows machine.
Here is what the karma.conf looks like:
(function () {
'use strict';
var exportedConf = require('./build.js');
var userConfig = exportedConf.userConfig;
module.exports = function (config) {
config.set({
// Karma configuration
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ["jasmine"],
// list of files / patterns to load in the browser
files: [
'../' + userConfig.build_dir + '/js/top/vendor/dev/**/*.js',
//'../' + userConfig.build_dir + '/js/top/project-root/**/*.js',
'../' + userConfig.src_dir + '/fragments/config.js',
'../' + userConfig.build_dir + '/js/bottom/vendor/dev/**/*.js',
'../' + userConfig.build_dir + '/js/bottom/project-root/**/*.js',
'../test/mockFactory.js',
'../test/jasmineVersionCheck.js',
'../' + userConfig.project_dir + '/**/' + userConfig.tests_folderName + '/**/*.spec.js'
],
// list of files to exclude
exclude: [
],
preprocessors: {
// preprocessors are defined at the end of file so that we can use the userConfig variables in the key
},
// test results reporter to use
reporters: ['progress', 'junit', 'coverage'],
coverageReporter: {
dir: '../' + userConfig.reports_dir + '/',
reporters: [
{
type: 'cobertura',
file: 'coverage.xml'
},
{
type: 'html',
file: 'coverage.html'
}
]
},
// web server port
port: process.env.KARMA_PORT || 8080,
// cli runner port
runnerPort: process.env.KARMA_RUNNER_PORT || 9100,
junitReporter: {
outputFile: '../' + userConfig.reports_dir + '/test-results.xml'
},
// enable / disable colors in the output (reporters and logs)
colors: process.env.KARMA_COLORS || true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [process.env.KARMA_BROWSER || 'PhantomJS'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 5000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true,
plugins: ['karma-jasmine', 'karma-phantomjs-launcher', 'karma-junit-reporter', 'karma-coverage']
});
// polyfills need to be excluded or instanbul instrumentation goes wild and screws it all!
config.preprocessors['../' + userConfig.build_dir + '/js/bottom/project-root/**/!(*-polyfills)+(.js)'] = ['coverage'];
};
}());
I hope someone can give me a hint on the matter, I have exhausted all the options I could think of.
Maybe, i really say, maybe, it might be due to the camel cased file names. IOs doesn't consider the case when it comes to file names.
I will edit my answer if i can come to any better idea.

Upgrading to AngularJS 1.3 from 1.2.6 unit test errors

I've been trying to upgrade our AngularJS app from 1.2.6 to 1.3.5 but have consistently been getting errors when trying to run the tests and the errors occur at test initialization. Further investigation proves that the tests are not even hit at the insertion points, rather fail just after initialization.
We are using Jasmine 1.x and karma test runner with Chrome as the browser. We are also using grunt to initiate the tests in this sequence:
grunt.registerTask('test', function () {
console.log(config);
grunt.task.run([
'jsbeautifier',
'newer:jshint',
'clean:server',
'autoprefixer',
'replace:htmlcommon',
'replace:htmlenv',
'replace:jscommon',
'replace:jsenv',
'to_single_quotes',
'connect:test',
'karma'
]);
});
Here is the error log:
LOG: 'WARNING: Tried to load angular more than once.'
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular/angular.js:25917
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-resource/angular-
resource.js:8
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-sanitize/angular-
sanitize.js:8
Chrome 39.0.2171 (Mac OS X 10.10.1) ERROR
Uncaught TypeError: undefined is not a function
at /Users/devUser/Development/WebAppGroup/webapp/app/bower_components/angular-route/angular-
route.js:26
Chrome 39.0.2171 (Mac OS X 10.10.1): Executed 0 of 0 ERROR (0.391 secs / 0 secs)
Excerpt of dependencies form our karma config file:
'app/scripts/common/googlemaps.js',
'app/bower_components/angular/angular.js',
'app/bower_components/angular-translate/angular-translate.js',
'app/bower_components/angular-translate-loader-partial/angular-translate-loader-partial.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/momentjs/moment.js',
'app/bower_components/angular-touch/angular-touch.js',
'app/bower_components/angular-carousel/dist/angular-carousel.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'app/bower_components/angular-scroll/angular-scroll.js',
'app/bower_components/jquery/dist/jquery.min.js',
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angularitics/src/angulartics.js',
'app/bower_components/angularitics/src/angulartics-adobe.js',
'app/bower_components/angularitics/src/angulartics-chartbeat.js',
'app/bower_components/angularitics/src/angulartics-flurry.js',
'app/bower_components/angularitics/src/angulartics-ga-cordova.js',
'app/bower_components/angularitics/src/angulartics-ga.js',
'app/bower_components/angularitics/src/angulartics-gtm.js',
'app/bower_components/angularitics/src/angulartics-kissmetrics.js',
'app/bower_components/angularitics/src/angulartics-mixpanel.js',
'app/bower_components/angularitics/src/angulartics-piwik.js',
'app/bower_components/angularitics/src/angulartics-scroll.js',
'app/bower_components/angularitics/src/angulartics-segmentio.js',
'app/bower_components/angularitics/src/angulartics-splunk.js',
'app/bower_components/angularitics/src/angulartics-woopra.js',
'app/bower_components/angularitics/src/angulartics-marketo.js',
'app/bower_components/ngstorage/ngStorage.js',
'app/bower_components/ng-clip/src/ngClip.js',
'app/bower_components/zeroclipboard/dist/ZeroClipboard.js',
UPDATED Here is the entire karma config file:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine', 'ng-scenario'],
// list of files / patterns to load in the browser
files: [
'app/scripts/common/googlemaps.js',
'app/bower_components/angular/angular.js',
'app/bower_components/angular-translate/angular-translate.js',
'app/bower_components/angular-translate-loader-partial/angular-translate-loader-partial.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/momentjs/moment.js',
'app/bower_components/angular-touch/angular-touch.js',
'app/bower_components/angular-carousel/dist/angular-carousel.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'app/bower_components/angular-scroll/angular-scroll.js',
'app/bower_components/jquery/dist/jquery.min.js',
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/angularitics/src/angulartics.js',
'app/bower_components/angularitics/src/angulartics-adobe.js',
'app/bower_components/angularitics/src/angulartics-chartbeat.js',
'app/bower_components/angularitics/src/angulartics-flurry.js',
'app/bower_components/angularitics/src/angulartics-ga-cordova.js',
'app/bower_components/angularitics/src/angulartics-ga.js',
'app/bower_components/angularitics/src/angulartics-gtm.js',
'app/bower_components/angularitics/src/angulartics-kissmetrics.js',
'app/bower_components/angularitics/src/angulartics-mixpanel.js',
'app/bower_components/angularitics/src/angulartics-piwik.js',
'app/bower_components/angularitics/src/angulartics-scroll.js',
'app/bower_components/angularitics/src/angulartics-segmentio.js',
'app/bower_components/angularitics/src/angulartics-splunk.js',
'app/bower_components/angularitics/src/angulartics-woopra.js',
'app/bower_components/angularitics/src/angulartics-marketo.js',
'app/bower_components/ngstorage/ngStorage.js',
/* 'app/bower_components/ng-clip/src/ngClip.js',
'app/bower_components/zeroclipboard/dist/ZeroClipboard.js',*/
'app/scripts/directives/setup.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
reporters: ['progress', 'coverage'],
preprocessors: {
'app/scripts/**/*.js': ['coverage']
}
});
};
Basically upon installing all the dependencies via bower, bower complains that several of the dependencies don't agree with AngularJS 1.3.x, but rather it needs 1.2.x where x = minor version required by individual dependency. I've tried doing the 'majority rules' take and given 1.2.27 (where most of the dependencies were asking for that particular version) and I've tried brute force 1.3.5 upgrade.
As a last resort, I've also applied the '*' to the dependency in my bower.json file to get the latest version of each dependency to use with AngularJS 1.3.5, however this doesn't work as well.
The feature set works as I can start my angular app and work through most of the features, but when it comes to ur BDD tests, this fails.
I'm stuck at this point as I've tried all possible paths. I think this issue has something related to the extensive set of libraries we are using and it's individual requirements for an older version of AngularJS. Also, why is it trying to load AngularJS more than once? I've read up on this being a possibility due to bad or missing routes but our routes work well when the application is started up.
Any thoughts?
Thanks,
Ben.

Karma tests does not seem to load my Angularjs controller

I run my angular tests with karma, my application is running fine in browser, but tests fails and I am suspecting wrong settings.
Here are the controllers and tests :
// app/scripts/controllers/main.js
'use strict';
angular.module('GloubiBoulgaClientApp')
.controller('MainCtrl', function ($scope) {
});
Here is the test file :
'use strict';
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('GloubiBoulgaClientApp'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(true).toBe(true);
});
});
The karma conf
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
The error ouput
PhantomJS 1.9.2 (Linux) Controller: MainCtrl should attach a list of awesomeThings to the scope FAILED
Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.8-build.2094+sha.b6c42d5/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got% 2
0undefined
at assertArg (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angula
r.js:1362)
at assertArgFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angu
lar.js:1373)
at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.js:6763
at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/test/spec/controllers/main.js:15
at invoke (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.j
s:3704)
at workFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular-mocks/angular -mocks.js:2120)
I am wondering why this happen, I tried to find some documentation about the karma initialization with angularjs. But the most documentation I found is only dummy tutorial that are repeating the same pattern ( like the dummy todo list, but with phones ... )
It seem that $controllerProvide.register fails to resolve my controllers name.
But Directives tests are working correctly ...
Thanks for your attention.
Edit Notes : I replaced the controller PersonCtrl by MainCtrl in this thread because It was confusing people about where to look. Now MainCtrl is the simpliest failing example I found.
This issue is only affecting my controllers, ( all of them ), but tests for Services and Directives are working as expected
I solved my problem, I've spent nearly a week to figure why this was not working.
I'd like to warn you, that Karma Stacktrace and error reports, even in debug mode, were not showing clues and were mainly missleading.
I've spent time in javascript debugger jumping frame to frame, to understand why my controllers where not loaded. ( Inspecting Angular's controllers register, shown it was empty)
While digging in my directories I've found a *.js that were not loaded in the index in production but by the globbing pattern in tests.
It was my old http_interceptor service that I moved but did not trashed the file.
Removing this buggy file fixed the weird Karma/Jasmine/Angular behaviour.
Lesson learned :
Do not trust tests output ( but what should I trust then ? ).
Remove files you are not using/testing.
Thanks to everyone who tryied to solve this issue.
i think the main problem is coming from the karma conf :
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/scripts/**/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
removing the * and specifying files one by one in the correct order, because if one is loaded before another it can break.
Edit : Add your files in the same order as your index.html
looking at this I'm wondering if the error message is confusing things.
in the stack trace i can see
TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings.length')
and from the example it does seem as though you have not defined any properties on the scope in your controller.
do you still have the problem if you add
$scope.awesomeThings = [];
to your controller?
If you're using the latest angular and also using the angular-route module, you should include the angular-route script in the karma conf file too:
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'app/scripts/**/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
I had this problem and adding it to the karma file did the trick.

Resources