gpt4 book ai didi

unit-testing - angularjs - 测试 Controller

转载 作者:行者123 更新时间:2023-12-04 04:13:12 26 4
gpt4 key购买 nike

我刚从 angular 开始,我想为我的 Controller 编写一些简单的单元测试,这就是我得到的。

应用程序.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, $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");
};
}

现在我正在将全局函数 Controller 转换为由 Angular API 定义的 Controller ,如 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});应该将我新创建的范围注入(inject)我的 DashboardController用属性填充它 - 在这种情况下, repeats .

最佳答案

您需要先设置原型(prototype)模块。

beforeEach(module('Prototype'));

将其添加到您的测试中,高于当前的 beforeEach会工作。
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);
});
});

关于unit-testing - angularjs - 测试 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com