如何编写具有多个参数的控制器的单元测试用例?

时间:2019-09-16 10:38:36

标签: angularjs unit-testing karma-jasmine

superusermodule.controller('SuperUserController', [
    '$scope',
    '$http',
    '$rootScope',
    '$state',
    '$localStorage',
    'Idle',
    '$timeout',
    '$window',
    'genericService',
    'superUserService',
    'SUPERUSERCONSTANTS', 'APPLICATIONCONSTANTS', '$filter', '$sessionStorage', '$interval',
    function ($scope, $http, $rootScope, $state, $localStorage, Idle, $timeout, $window, genericService, superUserService,
        SUPERUSERCONSTANTS, APPLICATIONCONSTANTS, $filter, $sessionStorage, $interval) {

$scope.someFunction = function(value){
...
return;
}

});

我有一个如上所述的控制器。我想为$scope中声明的函数编写测试用例。

我已经开始如下所示的测试用例,但是遇到TypeError: $controller is not a function错误。

describe('some test', function () {
    beforeEach(module('superusermodule'));
    var $controller, $http, $rootScope, $state, $localStorage, Idle, $timeout, $window, genericService, superUserService,
        SUPERUSERCONSTANTS, APPLICATIONCONSTANTS, $filter, $sessionStorage, $interval;

    beforeEach(inject(function (_$controller_, _$http_, _$rootScope_, _$state_, _$localStorage_, _Idle_, _$timeout_, _$window_,
        _genericService_, _superUserService_, _SUPERUSERCONSTANTS_, _APPLICATIONCONSTANTS_, _$filter_, _$sessionStorage_, _$interval_) {
        $controller = _$controller_;
        $http = _$http_;
        $rootScope = _$rootScope_;
        $state = _$state_;
        $localStorage = _$localStorage_;
        Idle = _Idle_;
        $timeout = _$timeout_;
        $window = _$window_;
        genericService = _genericService_;
        superUserService = _superUserService_;
        SUPERUSERCONSTANTS = _SUPERUSERCONSTANTS_;
        APPLICATIONCONSTANTS = _APPLICATIONCONSTANTS_;
        $filter = _$filter_;
        $sessionStorage = _$sessionStorage_;
        $interval = _$interval_;
    }));

    describe('$scope.someFunction', function () {
        it('someFunction test', function () {
            var $scope = {};
            var controller = $controller('SuperUserController', {
                $scope: $scope, $http: $http, $rootScope: $rootScope, $state: $state, $localStorage: $localStorage, Idle: Idle, $timeout: $timeout,
                $window: $window, genericService: genericService, superUserService: superUserService, SUPERUSERCONSTANTS: SUPERUSERCONSTANTS,
                APPLICATIONCONSTANTS: APPLICATIONCONSTANTS, $filter: $filter, $sessionStorage: $sessionStorage, $interval: $interval
            });
            expect($scope.someFunction("abc")).toEqual(null);
        });
    });

});

0 个答案:

没有答案