Testing a controller not using $scope using karma

試著忘記壹切 提交于 2019-12-24 11:28:13

问题


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


来源:https://stackoverflow.com/questions/30401371/testing-a-controller-not-using-scope-using-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!