How to write testable controllers with private methods in AngularJs? How to write testable controllers with private methods in AngularJs? angularjs angularjs

How to write testable controllers with private methods in AngularJs?


The controller function you provided will be used by Angular as a constructor; at some point it will be called with new to create the actual controller instance. If you really need to have functions in your controller object that are not exposed to the $scope but are available for spying/stubbing/mocking you could attach them to this.

function Ctrl($scope, anyService) {  $scope.field = "field";  $scope.whenClicked = function() {    util();  };  this.util = function() {    anyService.doSmth();  }}

When you now call var ctrl = new Ctrl(...) or use the Angular $controller service to retrieve the Ctrl instance, the object returned will contain the util function.

You can see this approach here: http://jsfiddle.net/yianisn/8P9Mv/


Namespacing it on the scope is pollution. What you want to do is extract that logic into a separate function which is then injected into your Controller. i.e.

function Ctrl($scope, util) {   $scope.field = "field";   $scope.whenClicked = function() {      util();   };}angular.module("foo", [])       .service("anyService", function(...){...})       .factory("util", function(anyService) {              return function() {                     anyService.doSmth();              };       });

Now you can unit test with mocks your Ctrl as well as "util".


I'm going to chime in with a different approach. You shouldn't be testing private methods. That's why they are private - it's an implementation detail that is irrelevant for the usage.

For example, what if you realize that util was used in several places but now, based on other code refactoring, it's only called in this one place. Why have an extra function call? Just include anyService.doSmith() inside you $scope.whenClicked() With the suggestions above, assuming you are testing that util() is called, your tests will break even though you haven't changed the functionality of the program. One of the main values of unit testing is to simplify refactoring without breaking things, so if you didn't break things, the test shouldn't fail.

What you need to do is ensure that when $scope.whenClicked is called, anyService.doSmth() is also called. You just need:

spyOn(anyService,'doSmith')scope.whenClicked();expect(anyService.doSmith).toHaveBeenCalled();