2

I've been looking through countless SO post on this but can't get the unit test of my angular controller to work.

Error:

TypeError: Object #<Object> has no method 'apply'
Error: [ng:areq] Argument 'CorrMatrixCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.14/ng/areq?p0=CorrMatrixCtrl&p1=not%20a%20function%2C%20got%20undefined
    at /home/sebastian/gutenberg-for-good/app/lib/angular/angular.js:78:12
    at assertArg (/home/sebastian/gutenberg-for-good/app/lib/angular/angular.js:1402:11)
    at assertArgFn (/home/sebastian/gutenberg-for-good/app/lib/angular/angular.js:1412:3)
    at /home/sebastian/gutenberg-for-good/app/lib/angular/angular.js:6881:9
    at null.<anonymous> (/home/sebastian/gutenberg-for-good/test/unit/controllersSpec.js:28:20)
    at Object.invoke (/home/sebastian/gutenberg-for-good/app/lib/angular/angular.js:3762:17)
    at workFn (/home/sebastian/gutenberg-for-good/test/lib/angular/angular-mocks.js:2144:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (/home/sebastian/gutenberg-for-good/test/lib/angular/angular-mocks.js:2129:25)
    at null.<anonymous> (/home/sebastian/gutenberg-for-good/test/unit/controllersSpec.js:26:33)
    at /home/sebastian/gutenberg-for-good/test/unit/controllersSpec.js:1:1

Controller:

angular.module('gutenberg.controllers', ['ui.bootstrap'])
.controller('CorrMatrixCtrl', ['AnalysisFactory', '$scope', function(AnalysisFactory, $scope){

    $scope.get = function() {
        //something
    };

}]);

Test:

describe("corrMatrixCtrl test", function() {

    var fakeFactory = {
        // some code
    };

    beforeEach(angular.module("gutenberg.controllers"));

    it("should have a get method", inject(function($controller, $rootScope) { // line 26

        var controller = $controller("CorrMatrixCtrl", {AnalysisFactory: fakeFactory, $scope: $rootScope.$new()}); // line 28

        expect(angular.isFunction(controller.get)).toBe(true);
    }));

});

From all the tutorials I've been reading or watching and all the documentation I can't see the error. Any kind of help is highly appreciated!

Karma config:

module.exports = function(config){
    config.set({
    basePath : '../',

    files : [
      'app/lib/angular/angular.js',
      'app/lib/angular/angular-*.js',
      'test/lib/angular/angular-mocks.js',
      'app/js/**/*.js',
      'test/unit/controllersSpec.js'
    ],

    exclude : [
      'app/lib/angular/angular-loader.js',
      'app/lib/angular/*.min.js',
      'app/lib/angular/angular-scenario.js'
    ],

    autoWatch : true,

    frameworks: ['jasmine'],

    browsers : ['Chrome'],

    plugins : [
            'karma-chrome-launcher',
            'karma-firefox-launcher',
            'karma-jasmine'
            ],

})}
3
  • What if you remove the dependency on ui.bootstrap? Commented Mar 13, 2014 at 3:32
  • Also, I think you're misusing the it statement. As far as I know, it expects a function as the second parameter. Instead, you're passing the return value of the inject function, which is probably undefined. Commented Mar 13, 2014 at 3:34
  • I'm pretty sure using the inject function is this case is correct, since in Angular Seed it's done the same way. The dependency is necessary since i'm using Angular UI Bootstrap, although I realized that in another tutorial I tried without UI Bootstrap the injection works. But I don't see any cause in UI Bootstrap, especially since there's no issue reported. Commented Mar 13, 2014 at 3:56

1 Answer 1

1

First off, your defining the get method on the $scope, and not on the controller. So your test should be:

var scope = $rootScope.$new();
var controller = $controller("CorrMatrixCtrl", {AnalysisFactory: fakeFactory, $scope: scope});
expect(angular.isFunction(scope.get)).toBe(true);

If you wanted to define it on the controller, your controller code should be:

this.get = function() {
    //something
};

That probably doesn't solve your problem though. My guess is that you don't include the controller code in the test. Add an alert/console.log at the top of the controller file and rerun the test to make sure that it's there.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. I understand that the controller is missing and after adding console log there's no output in console, but I don't get WHY it is missing.
How are you running your tests? From Karma or just the browser?
I'm using karma. Debug logs show that all necessary files are included.
Try changing beforeEach(angular.module("gutenberg.controllers")); to beforeEach(module("gutenberg.controllers"));.
It's because module == angular.mock.module and not angular.module. Same as inject == angular.mock.inject.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.