Testing a controller not using $scope using karma

2019-08-06 20:06发布

问题:

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:

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