How to test private functions?

Asked

Viewed 279 times

0

I have a controller with some functions:

angular.module('teste.controllers')
.controller(TestCtrl, function($scope){
   var vm = this;

   vm.funcTest = function(){
    return 1 + 1;
   }

   function _testSoma2(){
     return 2 + 2;
   }
})

Now on the test :

describe('Controllers: Timetable', function () {
 var TestCtrl;
 var $controller;
 var $scope;

 beforeEach(module('teste'));

 beforeEach(inject(function($controller, $injector){ 
   $controller = $controller;
   TestCtrl = $controller('TestCtrl', {
        '$scope': $scope
      });
   }));
   it('testar function privada.', function () {
      //como testar?
      //TestCtrl._testSoma2() ??
      expect(4).toBe(4);
   });
});

How I can perform the test in function _testSoma2() ?

in vm I can $Scope.vm.funcTest() but in _testSoma2() I cannot .

I can call this function private by several other functions of Scope, but I want to test this function separately.

  • Can you give a clearer example? in this example this function is never used right?

  • Exactly, it can be used by a scope function. But I want to test it separately.

  • 2

    It is not possible to have access to the function separately, it is a limitation of the language. You could have a public method calling the function and testing this method, but then the private method loses some of the purpose...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.