AngularJS + Karma + Jasmine 控制器单元测试:

AngularJS + Karma + Jasmine Controller Unit testing :

我有以下单元测试代码,其中第一个测试通过,但第二个测试失败,我无法弄清楚。

单元测试代码

describe('List controller', function () {

    beforeEach(angular.mock.module('myApp'));

    var $controller, $rootScope, $scope, $state;

    beforeEach(inject(function (_$controller_, _$rootScope_) {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
    }));

    describe('Parameter Group List', function() {
        it('should exists', function () {
            $controller = $controller('baseController', {'$scope': $scope});
            expect($scope.prepareSidebarMenu).toBeDefined();
        });
        it('should exists', function () {
            $state = {};
            $scope.header = {};
            $state.current = {};
            var parameterGroups = '';
            $scope.header.title = '';
            $controller = $controller('listController', {'$scope': $scope, 'parameterGroups': parameterGroups });
            expect($scope.truncateCharacters).toBeDefined();
        });
    });
});

我正在尝试测试以下控制器。

基地控制器

(function () {
    'use strict';

    angular.module('PpmApp')
        .controller('baseController', ['$scope', '$injector', baseController]);

    function baseController($scope, $injector) {

        var $state = $injector.get('$state');

        $scope.header = {
            title: "Plan Parameter Manager"
        };
        $scope.sidebarMenuInfo = {
            name: $state.current.name,
            parameterGroupId: null,
            hideSidebar: true
        };
        $scope.prepareSidebarMenu = function (sidebarMenuInfo) {
            angular.extend($scope.sidebarMenuInfo, sidebarMenuInfo);
        };
    }
})();

列出控制器

(function () {
    'use strict';

    angular.module('PpmApp')
        .controller('listController', ['$scope', '$injector', 'parameterGroups', listController]);

    function listController($scope, $injector, parameterGroups) {
        var $state = $injector.get('$state');
        var apiService = $injector.get('apiService');
        var utilService = $injector.get('utilService');
        var constantsProvider = $injector.get('constantsProvider');

        $scope.header.title = "Parameter Manager Overview";
        $scope.prepareSidebarMenu({
            name: $state.current.name,
            hideSidebar: true
        });

        var titleMaxLength = 47;
        var descriptionMaxLength = 270;

        $scope.parameterGroups = parameterGroups;
        $scope.createPopupInfo = {};
        $scope.createPopupInfo.validationErrors = {};
        $scope.createPopupInfo.validationErrors.isError = false;
        $scope.createPopupInfo.years = utilService.populateYearsForParameterGroup();
        $scope.createPopupInfo.months = constantsProvider.months;

        $scope.truncateCharacters = function (text, type) {
            if (type == 'title' && text.length > titleMaxLength) {
                return text.substring(0, titleMaxLength) + '...';
            } else if (type == 'description' && text.length > descriptionMaxLength) {
                return text.substring(0, descriptionMaxLength) + '...';
            }
            return text;
        };
 }
})();

我的单元测试结果是

$ karma start
07 02 2018 12:41:00.734:WARN [karma]: No captured browser, open http://localhost                                                                                                                                  :9876/
07 02 2018 12:41:00.746:INFO [karma]: Karma v0.13.22 server started at http://lo                                                                                                                                  calhost:9876/
07 02 2018 12:41:00.751:INFO [launcher]: Starting browser Chrome
07 02 2018 12:41:02.946:INFO [Chrome 63.0.3239 (Windows 7 0.0.0)]: Connected on                                                                                                                                   socket doxoZRvKoJYdYozhAAAA with id 67949004

  parameterGroupListController
    Parameter Group List
      √should exists
      ×should exists
        TypeError: $scope.prepareSidebarMenu is not a function
            at new parameterGroupListController (C:/ZS/JIM-PPM/PlanManagerWeb/Ji                                                                                                                                  mPPM/app/controllers/parameter-group-list.js:14:16)
            at Object.instantiate (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/resources                                                                                                                                  /angular.js:5112:14)
            at $controller (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/resources/angula                                                                                                                                  r.js:11083:28)
            at C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/node_modules/angular-mocks/an                                                                                                                                  gular-mocks.js:2314:14
            at UserContext.<anonymous> (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/test                                                                                                                                  s/parameter-group.spec.js:24:27)


Chrome 63.0.3239 (Windows 7 0.0.0): Executed 2 of 2 (1 FAILED) (0.038 secs / 0 s                                                                                                                                  ecs)
TOTAL: 1 FAILED, 1 SUCCESS


1) should exists
     parameterGroupListController Parameter Group List
     TypeError: $scope.prepareSidebarMenu is not a function
    at new parameterGroupListController (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/app                                                                                                                                  /controllers/parameter-group-list.js:14:16)
    at Object.instantiate (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/resources/angular                                                                                                                                  .js:5112:14)
    at $controller (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/resources/angular.js:110                                                                                                                                  83:28)
    at C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/node_modules/angular-mocks/angular-mo                                                                                                                                  cks.js:2314:14
    at UserContext.<anonymous> (C:/ZS/JIM-PPM/PlanManagerWeb/JimPPM/tests/parame                                                                                                                                  ter-group.spec.js:24:27)

listController 期望 prepareSidebarMenu 是在范围内定义的,但事实并非如此。如果它应该由 baseController 定义并从父作用域继承,它应该在 listController test:

中存根
$scope.prepareSidebarMenu = jasmine.createSpy();
$controller = $controller('listController', {'$scope': $scope, 'parameterGroups': parameterGroups });
expect($scope.prepareSidebarMenu).toHaveBeenCalledWith(...);
expect($scope.truncateCharacters).toBeDefined();