title: Testing AngularJS Service - mocking $rootElement and $rootScope
body: <p>I am currently writing some tests for a service that updates the meta information on a page.</p>
  
  <pre><code>mod.service('MetaSrv', ['$rootScope', '$rootElement', function ($rootScope, $rootElement){
  
  return {
      updateMetaTitle: function(contents) {
          $rootScope.metaTitle = contents;
      },
      updateMetaElement: function(type, contents) {
          var metaEl = angular.element($rootElement.find('meta[name="'+ type +'"]')[0]);
          metaEl.attr('content', contents);
      }
  };
  }]);
  </code></pre>
  
  <p>I want to test each of these functions as a starting point. The tests look like this:</p>
  
  <pre><code>describe('sets', function() {
  
      it('meta title element using $rootscope', function() {
          MetaSrv.updateMetaTitle('Some Title');
          expect(scope.metaTitle).toBe('Some Title');
      });
  
      it('meta description element using $rootElement', function() {
          MetaSrv.updateMetaElement('description', 'Some Description');
          var el = angular.element(rootElement.find('meta[name="description"]')[0]);
          expect(el.attr('content')).toBe('Some Description');
      });
  });
  </code></pre>
  
  <p>I need to be able to use <code>$rootScope</code> for the first test and then will need access to <code>$rootElement</code> for the second test. The describe block is as follows:</p>
  
  <pre><code>var MetaSrv, scope, rootElement, element;
  
  beforeEach(function() {
  
      module('mod');
  
      inject(function (_MetaSrv_, $rootScope, $rootElement) {
          MetaSrv = _MetaSrv_;
          scope = $rootScope.$new();
          rootElement = $rootElement;
      });
  });
  </code></pre>
  
  <p>Currently the first test works fine. The second test that uses <code>$rootElement</code> is not working correctly, it cannot find the meta tags on <code>$rootElement</code>. I am not sure how to mock <code>$rootElement</code> dependancy in order to test the meta information change.</p>