加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – angularjs – 测试控制器

发布时间:2020-12-17 06:57:34 所属栏目:安全 来源:网络整理
导读:我刚刚开始使用angular,我想为我的控制器编写一些简单的单元测试,这是我得到的. app.js: 'use strict';// Declare app level module which depends on filters,and servicesangular.module('Prototype',['setsAndCollectionsService']). config(['$routePro
我刚刚开始使用angular,我想为我的控制器编写一些简单的单元测试,这是我得到的.

app.js:

'use strict';


// Declare app level module which depends on filters,and services
angular.module('Prototype',['setsAndCollectionsService']).
  config(['$routeProvider',function($routeProvider) {
    $routeProvider.when('/dashboard',{templateUrl: 'partials/dashboard.html',controller: 'DashboardController'});
    $routeProvider.when('/setsAndCollections',{templateUrl: 'partials/setsAndCollections.html',controller: SetsAndCollectionsController});
    $routeProvider.when('/repetition',{templateUrl: 'partials/repetition.html',controller: RepetitionController});
    $routeProvider.otherwise({redirectTo: '/dashboard'});
  }]);

和controllers.js

'use strict';

/* Controllers */

    var myApp = angular.module('Prototype');

    myApp.controller('DashboardController',['$scope',function (scope) {
        scope.repeats = 6;
    }]);

/*function DashboardController($scope) {
 $scope.repeats = 5;
 };*/

function SetsAndCollectionsController($scope,$location,collectionsService,repetitionService) {
    $scope.id = 3;
    $scope.collections = collectionsService.getCollections();
    $scope.selectedCollection;
    $scope.repetitionService = repetitionService;

    $scope.switchCollection = function (collection) {
        $scope.selectedCollection = collection;
    };

    $scope.addCollection = function () {
        $scope.collections.push({
            name: "collection" + $scope.id,sets: []
        });
        ++($scope.id);
    };

    $scope.addSet = function () {
        $scope.selectedCollection.sets.push({
            name: "set" + $scope.id,questions: []
        });
        ++($scope.id);
    };

    $scope.modifyRepetition = function (set) {
        if (set.isSelected) {
            $scope.repetitionService.removeSet(set);
        } else {
            $scope.repetitionService.addSet(set);
        }

        set.isSelected = !set.isSelected;
    };

    $scope.selectAllSets = function () {
        var selectedCollectionSets = $scope.selectedCollection.sets;

        for (var set in selectedCollectionSets) {
            if (selectedCollectionSets[set].isSelected == false) {
                $scope.repetitionService.addSet(set);
            }
            selectedCollectionSets[set].isSelected = true;
        }
    };

    $scope.deselectAllSets = function () {
        var selectedCollectionSets = $scope.selectedCollection.sets;

        for (var set in selectedCollectionSets) {
            if (selectedCollectionSets[set].isSelected) {
                $scope.repetitionService.removeSet(set);
            }
            selectedCollectionSets[set].isSelected = false;
        }
    };

    $scope.startRepetition = function () {
        $location.path("/repetition");
    };
}

function RepetitionController($scope,repetitionService) {
    $scope.sets = repetitionService.getSets();
    $scope.questionsLeft = $scope.sets.length;
    $scope.questionsAnswered = 0;
    $scope.percentageLeft = ($scope.questionsLeft == 0 ? 100 : 0);

    $scope.endRepetition = function () {
        $location.path("/setsAndCollections");
    };
}

现在我正在将全局函数控制器转换为由角度API定义的控制器,正如您可以通过DashboardController的示例看到的那样.

现在在我的测试中:

describe("DashboardController",function () {
    var ctrl,scope;

    beforeEach(inject(function ($rootScope,$controller) {
        scope = $rootScope.$new();
        ctrl = $controller('DashboardController',{$scope: scope});
    }));

    it("has repeats attribute set to 5",function () {
        expect(scope.repeats).toBe(5);
    });
});

我正进入(状态

Error: Argument 'DashboardController' is not a function,got undefined

我想知道,我的错误在哪里?如果我理解这一点,ctrl = $controller(‘DashboardController’,{$scope:scope});应该将我新创建的范围注入我的DashboardController,用属性填充它 – 在这种情况下,重复.

解决方法

您需要先设置Prototype模块.

beforeEach(module('Prototype'));

将它添加到您的测试中,高于当前的每个将工作的当前.

describe("DashboardController",function () {
  var ctrl,scope;

  beforeEach(module('Prototype'));

  beforeEach(inject(function ($rootScope,$controller) {
    scope = $rootScope.$new();
    ctrl = $controller('DashboardController',{$scope: scope});
  }));

  it("has repeats attribute set to 5",function () {
    expect(scope.repeats).toBe(5);
  });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读