Karma tests don't seem to load my Angularjs controller

I run angular tests with karma, my application works fine in the browser, but the tests fail, and I suspect the 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 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); }); }); 

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 }); }; 

Exit error

  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 happened, I tried to find some documentation on karma initialization using angularjs. But the biggest documentation I found is just a dummy tutorial that repeats the same pattern (e.g. list of numbered todo, but with phones ...)

It seems that $ controllerProvide.register cannot resolve my controller name. But directive tests work correctly ...

Thanks for attention.

Edit notes: I replaced the PersonCtrl controller with MainCtrl in this thread because it confused people about where to look. Now MainCtrl is the simplest unsuccessful example I found.

This problem only affects my controllers (all of them), but tests for services and directives work as expected

+7
angularjs testing controller karma-runner
source share
4 answers

I solved my problem, I spent almost a week to understand why this does not work.

I would like to warn you that Error Reports and Error Messages , even in debug mode, did not display prompts, but were mostly skipped . I spent time in a javascript debugger moving a frame into a frame to understand why my controllers are not loaded. (Checking Angular loggers shown empty)

While digging in my directories, I found * .js that were not loaded into the index during production, and the globbing pattern in tests.

This was my old http_interceptor service, which I moved but did not ruin the file. Removing this buggy file eliminated the strange behavior of Karma / Jasmine / Angular.

Lesson learned: Don't trust the test results (but what should I trust?). Delete files you are not using / not checking.

Thanks to everyone who tried to solve this problem.

+10
source share

I think the main problem comes from 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' ], 

delete * and specify files in turn in the correct order, because if one loads before the other, it may break.

Edit: add your files in the same order as index.html

+8
source share

looking at this, I wonder if the error message is confused.

in the stack trace i see

 TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings.length') 

and from the example it seems that you have not defined any properties in the area of ​​your controller.

You still have a problem if you add

 $scope.awesomeThings = []; 

to your controller?

+1
source share

If you use the latest angular , as well as using the angular -route module , you must include the angular -route script in the karma config file:

 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.

+1
source share

All Articles