title: Testing $scope in AngularJs controller with dependency on $filter
body: <p>I've gone through a few tutorials and basic examples but I'm having a hard time writing unit tests for my controller. I've seen code snippets instantiating controllers and letting angular inject the <code>$rootScope</code> object which in turn is used to create a new <code>scope</code> object for the controller. But I can't figure out why <code>ctrl.$scope</code>? <strong>is undefined</strong>:</p>
  
  <pre><code> describe('EmployeeCtrl', function () {
      var scope, ctrl, $httpBackend;
  
      beforeEach(inject(function (_$httpBackend_, $rootScope, $controller, $filter) {
          $httpBackend = _$httpBackend_;       
  
          scope = $rootScope.$new();
          ctrl = $controller('EmployeeCtrl', { $scope: scope});
          expect(ctrl).not.toBeUndefined();
          expect(scope).not.toBeUndefined();   //&lt;-- PASS!      
          expect(ctrl.$scope).not.toBeUndefined();  //&lt;-- FAIL!       
      }));
  });
  </code></pre>
  
  <p>I ended up using the <code>scope</code> variable instead of <code>ctrl.$scope</code> but then on my first test I couldn't figure out how to unit test a <strong>function variable</strong> inside my controller:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code> function EmployeeCtrl($scope, $http, $filter, Employee) {
    var searchMatch = function (haystack, needle) {
     return false;
    }
   }
  </code></pre>
  
  <p><strong>Broken unit test:</strong></p>
  
  <pre><code>it('should search ', function () {                
      expect(ctrl.searchMatch('numbers','one')).toBe(false);
  });
  </code></pre>
  
  <p>This is what I get</p>
  
  <blockquote>
    <p>TypeError: Object # has no method 'searchMatch'</p>
  </blockquote>
  
  <p>How do you test that function? As a workaround I moved my method to $scope so I could test for <code>scope.searchMatch</code> but I was wondering if this is the only way.</p>
  
  <p>Finally, on my tests is appears <code>$filter</code> <strong>is undefined too</strong>, how do you inject it? I tried this but didn't work:</p>
  
  <pre><code>ctrl = $controller('EmployeeCtrl', { $scope: scope, $filter: $filter });
  </code></pre>
  
  <p>Thanks</p>
  
  <p><strong>Update:</strong>
  The method mentioned above to inject $filter works just fine.</p>