Wie erstelle ich Unit Test für meinen AngularJS Controller LoginController? Wie man AuthenticationService für dieses Beispiel nachbildet? Ist mein Controller ordnungsgemäß für Unit-Tests & mit Mock-Ups gebaut?Wie erstellt man Unit Tests für Controller in AngularJS
-Controller LoginController Code:
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]);
}
}
}
}]);
von meinem Wissen :), es ist nicht Test runing :). Wie wäre es, den Authentifizierungsdienst zu verbessern? Keine Cerdentails gesetzt. Es funktioniert nicht :) –
Dies wird Ihre 'Login()' Funktion testen, die Ihr tatsächlicher Code ist. Sie können seine Auswirkungen auf $ scope oder andere Objekte wie $ location usw. erfassen. Können Sie beschreiben, was genau Sie als eine Testeinheit betrachten? – bhantol
Was ist mit Authentnization Service? es ist Abhängigkeit. Ich dachte mir, dass es nur zum Testen von Controllern gespottet werden sollte. Aber ich kann mich irren. –