title: Testing element directive - can't access isolated scope methods during tests
body: <p>I have the following directive. </p>
  
  <pre><code>directivesModule.directive('wikis', function() {
  var urlRegex = new RegExp('^(https?)://.+$');
  
  return {
      restrict: 'E', 
      templateUrl: 'templates/wiki-list.html',
      scope: {
          wikis: '='
      },
      link: function(scope, element, attrs) {
          scope.newWikiURL = '';
  
          scope.$watch('wikis', function() {
              if (scope.wikis == undefined || scope.wikis.length === 0) {
                  scope.class = 'hide';
              } else {
                  scope.class = 'show';
              }
          }, false);
  
          scope.addWiki = function() {
              if (scope.wikis == undefined) {
                  scope.wikis = [];
              }
  
              var nw = scope.newWikiURL;
              if (nw != undefined &amp;&amp; nw.trim() != '' &amp;&amp; urlRegex.exec(nw)) { 
                  scope.wikis.push(nw);
                  scope.newWikiURL = '';
              }
          }
  
      }
  };
  
  });
  </code></pre>
  
  <p>When I test it:</p>
  
  <pre><code>describe('Wikis Directive Test Suite', function() {
      var scope, elem, directive, linkFn, html;
  
      beforeEach(function() {
          html = '&lt;wikis wikis=''&gt;&lt;/wikis&gt;';
  
          inject(function($compile, $rootScope) {
              scope = $rootScope.$new();
              scope.wikis = [];
  
              elem = angular.element(html);
  
              $compile(elem)(scope);
  
              scope.$digest();
          });
  
      });
  
      it('add Wiki should add a valid wiki URL to artist', function() {
          var url = 'http://www.foo.com';
          scope.newWikiURL = url;
          scope.addWiki();
  
          expect(scope.wikis.length).toBe(1);
          expect(scope.wikis[0]).toBe(url);
          expect(scope.newWikiURL).toBe('');
      });
  });
  </code></pre>
  
  <p>I get an error saying that Object doesn't have an addWiki method. I tried to debug it, and the link function is not called during the test. I suspect that's why the addWiki method is not part of the scope. Anybody knows why I'm getting this error?</p>
  
  <p>By the way, Is it a normal practice to add some logic into the link function of a directive as it would be a Controller itself? Because looking at the code I know that it's why in reality I'm doing. </p>