1

I followed the instructions on docs.angularjs.org to test a controller with karma and it's perfectly working. However I was wondering if it's possible to test a controller which is not using $scope?

Testing this controller is ok:

angular.module('starter.controllers')
.controller('PasswordController', function PasswordController($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';
        }
    };
});

But I'd like to test:

angular.module('starter.controllers')
.controller('PasswordController', function PasswordController()
{
    var myController = this;
    myController.password = '';
    myController.grade = function()
    {
        var size = myController.password.length;
        if (size > 8) {
            myController.strength = 'strong';
        } else if (size > 3) {
            myController.strength = 'medium';
        } else {
            myController.strength = 'weak';
        }
    };
});

The testing code is the following:

describe('PasswordController', function()
{
    beforeEach(module('starter.controllers'));

    var $controller;

    beforeEach(inject(function(_$controller_){
        $controller = _$controller_;
    }));

    describe('$scope.grade', function() {
        var $scope, controller;

        beforeEach(function() {
            $scope = {};
            //This line doesn't work with the second controller
            controller = $controller('PasswordController', { $scope: $scope});
        });

        it('sets the strength to "strong" if the password length is >8 chars', function() {
            $scope.password = 'longerthaneightchars';
            $scope.grade();
            expect($scope.strength).toEqual('strong');
        });
    });
});
1
  • 1
    Why do you pass a $scope when constructing the controller, since the controller function doesn't take one as argument? Commented May 22, 2015 at 16:20

1 Answer 1

3

This should work:

describe('PasswordController', function()
{
    beforeEach(module('starter.controllers'));

    var $controller;

    beforeEach(inject(function(_$controller_){
        $controller = _$controller_;
    }));

    describe('$scope.grade', function() {
        var controller;

        beforeEach(function() {
            controller = $controller('PasswordController', {});
        });

        it('sets the strength to "strong" if the password length is >8 chars', function() {
            controller.password = 'longerthaneightchars';
            controller.grade();
            expect(controller.strength).toEqual('strong');
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.