0
如何爲我的AngularJS控制器LoginController創建單元測試?如何爲此示例模擬AuthenticationService? 我的控制器是否使用模型正確構建了單元測試&?如何在AngularJS中爲控制器創建單元測試
控制器的LoginController代碼:
layoutControllers.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
AuthenticationService.ClearCredentials();
$scope.login = function() {
$scope.dataLoading = true;
AuthenticationService.Login($scope.email, $scope.password, function (response) {
AuthenticationService.SetCredentials($scope.email, $scope.password, response.UserId);
$scope._showValidationErrors(null, null);
$location.path('/');
}, function (response) {
$scope._showValidationErrors(response.Errors, response.OtherErrors);
});
$scope.dataLoading = false;
};
$scope._showValidationErrors = function (errors, otherErrors) {
$scope.validationErrors = [];
$scope.errors = {};
$scope.errors.form = {};
if (errors && angular.isArray(errors)) {
for (var errorCounter in errors) {
$scope.validationErrors.push(errors[errorCounter].Message);
$scope.errors.form[errors[errorCounter].Key] = errors[errorCounter].Message;
}
// debugger;
}
if (otherErrors && angular.isArray(otherErrors)) {
for (var errorCounter2 in otherErrors) {
$scope.validationErrors.push(otherErrors[errorCounter2]);
}
}
}
}]);
從我的知識:),它不運行測試:)。嘲笑AU證書服務呢?沒有cerdentails設置。它不會工作:) –
這將測試你的'login()'函數,它是你的實際代碼。你可以捕獲它對$ scope或其他對象的影響,比如$ location等。你能描述一下你在考慮作爲一個被測試的單元嗎? – bhantol
認證服務怎麼樣?這是依賴性。我認爲它應該被嘲笑只測試控制器。但我可能是錯的。 –