title: How to test local functions within angular controllers?
body: <p>Let's suppose I have a controller like:</p>
  
  <pre><code>angular
      .module("app", [])
      .controller("NewsFeedController", [
          "$scope",
          "NewsFeedService",
          function ($scope, NewsFeedService) {
              $scope.news = [
                  { stamp: 1 },
                  { stamp: 9 },
                  { stamp: 0 }
              ];
  
              $scope.onScroll = function () {
                  /*
                      might do some stuff like debouncing, 
                      checking if there's news left to load, 
                      check for user's role, whatever. 
                  */
  
                  var oldestStamp = getOldestNews().stamp;
                  NewsFeedService.getOlderThan(oldestStamp);
  
                  /* handle the request, append the result, ... */
              };
  
              function getOldestNews () {
                  /* code supposed to return the oldest news */
              }
          }
      ]);
  </code></pre>
  
  <p><code>getOldestNews</code> is declared as a local function since there is no point to expose it in the <code>$scope</code>.</p>
  
  <p>How should I deal with it? How can I actually test this function?</p>
  
  <pre><code>describe("NewsFeedController", function () {
      beforeEach(module("app"));
  
      var $controller, $scope, controller;
  
      beforeEach(inject(function (_$controller_) {
          $controller = _$controller_;
          $scope      = {};
          controller  = $controller("NewsFeedController", { $scope: $scope });
      }));
  
      it("should return the oldest news", function () {
          // How do I test getOldestNews?
      });
  });
  </code></pre>
  
  <p>By the way, it'd be great if the solution also works for local functions within services and directives.</p>
  
  <hr>
  
  <p>Related questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/22921484/how-can-we-test-non-scope-angular-controller-methods">How can we test non-scope angular controller methods?</a></li>
  <li><a href="https://stackoverflow.com/questions/26938842/how-do-i-mock-local-variable-for-a-function-in-a-service-jasmine-karma-tests">How do I mock local variable for a function in a service? Jasmine/Karma tests</a></li>
  </ul>