0

I'm following the Angular tutorial on how to unit test a controller. I'm using Karma and Jasmine for testing. When I run the test, I get

Error: [ng:areq] Argument 'testController' is not a function, got undefined

Any ideas of how I can load the testController in test.js? Code below. Thanks!

<!-- app.js --> 

var simulatorApp = angular.module('simulatorApp', ['ngRoute', 'ngResource', 'ngCookies'],

simulatorApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {templateUrl: '/angular/views/home.html', controller: homeController})
        .when('/test', {templateUrl: '/angular/views/test.html', controller: testController})
        .otherwise({redirectTo: '/'});
}]);

<!-- testController.js -->

function testController($scope) {
    $scope.password = '';
    $scope.grade = function () {
        var size = $scope.password.length;
        if (size > 8) {
            $scope.strength = 'strong';
        } else if (size > 3) {
            $scope.strength = 'medium';
        } else {
            $scope.strength = 'weak';
        }
    };
}

<!-- test.js -->
describe('testController', function() {
    beforeEach(module('simulatorApp'));

    var $controller;

    beforeEach(inject(function (_$controller_) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $controller = _$controller_;
    }));

    describe('$scope.grade', function () {
        it('sets the strength to "strong" if the password length is >8 chars', function () {
            var $scope = {};
            var controller = $controller('testController', {$scope: $scope});
            $scope.password = 'longerthaneightchars';
            $scope.grade();
            expect($scope.strength).toEqual('strong');
        });
    });
})

2 Answers 2

1

Here's a working plunkr.

As Narek Mamikonyan answered you haven't registered your controller on your module.

simulatorApp.controller('testController', testController);

function testController($scope) {
    ...
};

Same problem likely with homeController

simulatorApp.controller('homeController', homeController);

function homeController($scope) {
    ...
};

Besides, you should try and declare your controller something like this as this won't explode if you have minification on your javascript files

simulatorApp.controller('testController', ['$scope', testController]);

If you don't, $scope won't be the same once minified and your app won't work.

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

Comments

0

you didn't defined your controller in module

simulatorApp.controller('testController', function testController($scope) {
    $scope.password = '';
    $scope.grade = function () {
        var size = $scope.password.length;
        if (size > 8) {
            $scope.strength = 'strong';
        } else if (size > 3) {
            $scope.strength = 'medium';
        } else {
            $scope.strength = 'weak';
        }
    };
})

1 Comment

Figured it out, I needed to reference my controller as a string in the $routeProvider. Thanks!

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.