Angular 单元测试控制器
Angular Unit Testing Controllers
我的控制器除了在服务中调用方法之外没有做很多其他事情,服务包装和 returns 它的功能,我已经为模拟 http 请求的服务编写了单元测试。
在这种情况下是否值得对控制器进行单元测试?如果是,我会测试什么,因为我已经测试了服务功能。
下面是我的控制器:
'use strict';
/* Controllers */
var calculatorControllers = angular.module('calculatorControllers', []);
calculatorControllers.controller('BodyController', ['$scope',
function($scope) {
$scope.toggleNavBarActive = function($event) {
$($event.currentTarget).parent().find('.active').removeClass('active');
$($event.currentTarget).addClass('active');
};
}]);
calculatorControllers.controller('CalculatorCtrl', ['$scope', 'CalculatorService',
function($scope, CalculatorService) {
$scope.orderProp = 'asc';
$scope.result = ' awaiting calculation';
$scope.sum = {};
$scope.add = function(val1, val2) {
var promise = CalculatorService.add(val1, val2);
promise.then(function(response) {
$scope.result = response;
});
};
}]);
calculatorControllers.controller('AboutCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {
}]);
控制器方法不需要测试的唯一情况是
$scope.calculator = CalculatorService;
所以像 {{ calculator.sum(...) }}
这样的所有视图调用都是由服务完成的。
在所有其他情况下,都应测试控制器方法。由于 CalculatorService
单元已经过测试,因此必须对其进行模拟,以便单独测试控制器逻辑。
Is it even worth unit testing the controller in this instance
是的,您应该以 100% 的覆盖率为目标,无论是控制器还是服务。我会在这里测试两件事 (Jasmine):
it('inits $scope', function() {
var $scope = {};
$controller('PasswordController', { $scope: $scope });
expect($scope.orderProp).toEqual('asc');
expect($scope.result).toEqual(' awaiting calculation');
expect($scope.sum).toEqual({});
});
it('calls CalculatorService and sets the result', function() {
var $scope = {};
$controller('PasswordController', { $scope: $scope });
$scope.sum(1, 2);
expect(CalculatorServiceMock).toHaveBeenCalledWith(1, 2);
resolveCalculatorServiceMockAddSpyWith(3);
expect($scope.result).toEqual(3);
});
我的控制器除了在服务中调用方法之外没有做很多其他事情,服务包装和 returns 它的功能,我已经为模拟 http 请求的服务编写了单元测试。
在这种情况下是否值得对控制器进行单元测试?如果是,我会测试什么,因为我已经测试了服务功能。
下面是我的控制器:
'use strict';
/* Controllers */
var calculatorControllers = angular.module('calculatorControllers', []);
calculatorControllers.controller('BodyController', ['$scope',
function($scope) {
$scope.toggleNavBarActive = function($event) {
$($event.currentTarget).parent().find('.active').removeClass('active');
$($event.currentTarget).addClass('active');
};
}]);
calculatorControllers.controller('CalculatorCtrl', ['$scope', 'CalculatorService',
function($scope, CalculatorService) {
$scope.orderProp = 'asc';
$scope.result = ' awaiting calculation';
$scope.sum = {};
$scope.add = function(val1, val2) {
var promise = CalculatorService.add(val1, val2);
promise.then(function(response) {
$scope.result = response;
});
};
}]);
calculatorControllers.controller('AboutCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {
}]);
控制器方法不需要测试的唯一情况是
$scope.calculator = CalculatorService;
所以像 {{ calculator.sum(...) }}
这样的所有视图调用都是由服务完成的。
在所有其他情况下,都应测试控制器方法。由于 CalculatorService
单元已经过测试,因此必须对其进行模拟,以便单独测试控制器逻辑。
Is it even worth unit testing the controller in this instance
是的,您应该以 100% 的覆盖率为目标,无论是控制器还是服务。我会在这里测试两件事 (Jasmine):
it('inits $scope', function() {
var $scope = {};
$controller('PasswordController', { $scope: $scope });
expect($scope.orderProp).toEqual('asc');
expect($scope.result).toEqual(' awaiting calculation');
expect($scope.sum).toEqual({});
});
it('calls CalculatorService and sets the result', function() {
var $scope = {};
$controller('PasswordController', { $scope: $scope });
$scope.sum(1, 2);
expect(CalculatorServiceMock).toHaveBeenCalledWith(1, 2);
resolveCalculatorServiceMockAddSpyWith(3);
expect($scope.result).toEqual(3);
});