I am new to jasmine testing and I could not get a solid picture about how I could unittest my controller. My controller does some assignments and I basically want test to do the following:
- Mock service that it calls.
- Mock data in scope.
- Test the behaviour of the particular method in controller.
- Verify that: service has been invoked once.
- Verify that: service was passed correct data.
I would need to have as complete as possible example how to write a test, and what declarations to use to mock the methods.
Controller method in question looks like this:
.controller('ProjectConfigurationCtrl', function($scope, $routeParams, InfraService, ProjectConfigurationService) {
// some pre-init...
$scope.clickUpdate = function() {
if ($scope.projectData.Id === 0) {
ProjectConfigurationService.save(angular.toJson($scope.projectData), function(project) {
$scope.projectData = project;
$scope.newproject = false;
$scope.info = "Project " + project.ProjectName + " created successfully.";
}, function() {
$scope.info = "Project " + $scope.projectData.ProjectName + " can not be created: server error.";
});
} else {
ProjectConfigurationService.update(angular.toJson($scope.projectData), function() {
$scope.info = "Project " + $scope.projectData.ProjectName + " updated successfully.";
}, function() {
$scope.info = "Project " + $scope.projectData.ProjectName + " can not be updated: server error.";
});
}
I want to see if ProjectConfigurationService.save/update was called once, and check what arrived to these calls as parameters. I want mocked service return information. After the call, I want to examine $scope.newproject, $scope.info etc. to see what is inside there.