title: How do I inject a mock dependency into an angular directive with Jasmine on Karma
body: <p>I have the following directive:</p>
  
  <pre><code>function TopLevelMenuDirective ($userDetails, $configuration) {
      return {
          restrict:'A',
          templateUrl: staticFilesUri + 'templates/TopLevelMenu.Template.html', 
          scope: {
              activeTab: '='
          },
          link: function (scope, element, attributes) {
              var userDetails = $userDetails;
              if ($userDetails) {
                  scope.user = {
                      name: userDetails.name ? userDetails.name : 'KoBoForm User',
                      avatar: userDetails.gravatar ? userDetails.gravatar: (staticFilesUri + '/img/avatars/example-photo.jpg')
                  };
              } else {
                  scope.user = {
                      name: 'KoBoForm User',
                      avatar: staticFilesUri + '/img/avatars/example-photo.jpg'
                  }
              }
  
              scope.sections = $configuration.sections();
  
              scope.isActive = function (name) {
                  return name === scope.activeTab ? 'is-active' : '';
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to mock the dependencies to unit test the different code paths with values known by the unit tests. I have the following sample unit test:</p>
  
  <pre><code>it('should set $scope.user to values passed by $userDetails', 
      inject(function($compile) {
          var element = '&lt;div top-level-menu&gt;&lt;/div&gt;';
          element = $compile(element)($scope);
          $scope.$apply();
  
          expect(element.isolateScope().user.name).toBe('test name');
          expect(element.isolateScope().user.avatar).toBe('test avatar');
      }
  ));
  </code></pre>
  
  <p>This gives me two problems.</p>
  
  <p>First, since the template is in an external file, when it loads it tries to fetch it and errors out beacause the file is nowhere to be found, which is logical since it's in a test environment and not an actual server.</p>
  
  <p>Second, there's no apparent way to mock the dependencies injected into the directive through its constructor. When testing controllers you can use the <code>$controller</code> service, but since directives are instantiated indirectly by compiling an html tag with a passed scope, there's no way to instantiate it directly (e.g. there's no analogous <code>$directive</code>). This impedes me from setting <code>$userDetails.name</code> and <code>$userDetails.gravatar</code> to <code>'test name'</code> and <code>'test avatar'</code> respectively. </p>
  
  <p>How do I get the directive to compile properly and run with a custom $userDetails dependency?</p>