title: 'undefined is not an object' in karma/jasmine unit test
body: <p>I have the following simple unit test:</p>
  
  <pre><code>describe('SyncController', function() {
    var controller,
        deferredRecount,
        pouchdbServiceMock;
  
    beforeEach(module('inspector'));
  
    beforeEach(inject(function($controller, $q, $scope) {
      deferredRecount = $q.defer();
  
      pouchdbServiceMock = {
        getRecordCounts: jasmine.createSpy('getRecordCounts spy').and.returnValue(deferredRecount.promise)
      };
  
      controller = $controller('SyncController', {
        '$scope': $scope,
        'pouchdbService': pouchdbServiceMock
      });
    }));
  
    beforeEach(inject(function(_$rootScope_) {  
      $rootScope = _$rootScope_;
      controller.recount();
    }));
  
    describe('recount', function() {
      it('should call getRecordCounts on pouchdbService', function() {
        expect(pouchdbServiceMock.getRecordCounts).toHaveBeenCalled();
      });
    });
  });
  </code></pre>
  
  <p>Calling <code>pouchdbServiceMock.getRecordCounts</code> in the last <code>describe</code> block generates an error:</p>
  
  <pre><code>TypeError: undefined is not an object (evaluating 'pouchdbServiceMock.getRecordCounts') in unit-tests/sync.controller.tests.js (line 29)
  </code></pre>
  
  <p>However <code>pouchdbServiceMock</code> is assigned in the second <code>beforeEach</code> block. What is wrong?</p>