title: Mocking $mdSideNav in unit test
body: <p>I have a simple enough function that closes an $mdSidenav instance in my application</p>
  <pre><code>function closeSideNav() {
      $mdSidenav('left').close();
  }
  </code></pre>
  <p>I'm now needing to unit test this, but am having trouble writing an expectation for the <code>close()</code> call on $mdSidenav.</p>
  <p>I thought about using $provide in my test spec</p>
  <pre><code>module(function($provide) {
      $provide.value('$mdSidenav', function(id) {
          return {
              close: jasmine.createSpy('$mdSidenav.close')
          }
      })
  });
  
  beforeEach(inject(function(_$controller_, _$mdSidenav_) {
      $controller = _$controller_;
      $mdSidenav = _$mdSidenav_;
  }));
  
  beforeEach(function() {
      vm = $controller('NavbarController', {
          $mdSidenav: $mdSidenav
      });
  });
  
  describe('vm.closeSideNav', function() {
      beforeEach(function() {
          spyOn($mdSidenav, 'close');
          vm.closeSideNav()
      });
      it('should call $mdSidenav.close()', function() {
          expect($mdSidenav.close).toHaveBeenCalled();
      });
  });
  </code></pre>
  <p>This throws a couple of errors:</p>
  <blockquote>
  <p>Error: close() method does not exist</p>
  <p>Error: Expected a spy, but got undefined.</p>
  </blockquote>
  <p>Has anyone managed to mock out $mdSidenav and offer me some guidance please?</p>
  <p>Thanks</p>
  <p><strong>UPDATE</strong></p>
  <p>Based on the suggested answer, I have now updated my test spec to</p>
  <pre><code>'use strict';
  describe('NavbarController', function() {
    var $controller,
      vm,
      $mdSidenav,
      sideNavCloseMock;
    beforeEach(function() {
      module('app.layout');
      sideNavCloseMock = jasmine.createSpy();
      module(function($provide) {
        $provide.value('$mdSidenav', function() {
          return function(sideNavId) {
            return {close: sideNavCloseMock}
          }
        })
      });
  
    });
    beforeEach(inject(function(_$controller_, _$mdSidenav_) {
      $controller = _$controller_;
      $mdSidenav = _$mdSidenav_;
    }));
  
    beforeEach(function() {
      vm = $controller('NavbarController', {
        $mdSidenav: $mdSidenav
      });
    });
    describe('vm.closeSideNav', function() {
      beforeEach(function() {
        vm.closeSideNav()
      });
      it('should call $mdSidenav.close()', function() {
        expect(sideNavCloseMock).toHaveBeenCalled();
      });
    });
  });
  </code></pre>
  <p>And for a sanity check, my actual controller looks as follows:</p>
  <pre><code>(function () {
    'use strict';
  
    angular
      .module('app.layout')
      .controller('NavbarController', Controller);
  
    Controller.$inject = ['$mdSidenav'];
  
    function Controller($mdSidenav) {
      var vm = this;
      vm.closeSideNav = closeSideNav;
  
      //This only affects the sideNav when its not locked into position, so only on small\medium screens
      function closeSideNav() {
        $mdSidenav('left').close();
      }
    }
  })();
  </code></pre>
  <p>Unfortunately this still isn't working for me, and I end up with a different error</p>
  <pre><code>TypeError: undefined is not a constructor (evaluating '$mdSidenav('left').close())
  </code></pre>