angularjs - 测试控制器 [英] angularjs - testing controller

查看:117
本文介绍了angularjs - 测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始用的角度,我想写一些简单的单元测试我控制器,这里是我得到了什么。

I am just starting with angular and I wanted to write some simple unit tests for my controllers, here is what I got.

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

and 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, $location, 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 见的过程。

now I am in process of converting global function controllers to ones defined by angular API as you can see by example of 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 = $控制器('DashboardController',{$范围:适用范围}); 应注入我新创建的范围,我的 DashboardController 与属性填充它 - 在这种情况下,重复

I am wondering then, where is my mistake? If I understand this right, ctrl = $controller('DashboardController', {$scope: scope}); should inject my newly created scope to my DashboardController to populate it with attributes - in this case, repeats.

推荐答案

您需要先设置您的原型模块。

You need to set up your Prototype module first.

beforeEach(module('Prototype'));

这是添加到您的测试,上面的电流 beforeEach 会工作。

Add that to your test, above the current beforeEach would work.

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

这篇关于angularjs - 测试控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆