0

My controller doesnt do a lot other than call methods in a service, the service wraps up and returns its functions, I have already written unit tests for the service mocking the http request.

Is it even worth unit testing the controller in this instance and if so what would I be testing as I have already tested the service functionality.

Below is my controller:

'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) {

}]);
6
  • Please post the code of your controller. Otherwise, the question is too broad. Commented Apr 8, 2016 at 14:03
  • In my app, most of my services make requests to APIs and do work on the response before handing the promise back to the controller. My controllers usually do minimal work on the response before the template consumes the data. There are some cases that I modify the data before then, and in that case it might warrant unit testing the response. It really depends on how easy and modular your controller APIs are. BTW do you do jujitsu? Just asking because of your username. Commented Apr 8, 2016 at 14:07
  • Hey yes I do jiu jitsu, do you? I am a purple belt at the moment. I wish I was a purple belt in Angular but I feel like a white no stripe... Commented Apr 8, 2016 at 14:08
  • As requested by @Pavlo can you include the controller in question... Commented Apr 8, 2016 at 14:09
  • At the moment I just want to test the calculator controller, I will worry about the body controller once I know roughly what I am doing. Commented Apr 8, 2016 at 14:12

2 Answers 2

1

Is it even worth unit testing the controller in this instance

Yes, you should aim for 100% coverage, not matter controller or service. I would test two things here (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);
});
Sign up to request clarification or add additional context in comments.

Comments

1

The only case when the controller methods don't require testing is

$scope.calculator = CalculatorService;

So all view calls like {{ calculator.sum(...) }} are done by the service.

In every other case controller methods should be tested. Since CalculatorService unit was already tested, it has to be mocked in order for controller logic to be tested in isolation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.