title: How to unit test an AngularJS service / factory
body: <p>I'm already some time in the development using AngularJS, and what I do write works, but know I've come to a point where I would like to run unit tests on my AngularJS code.</p>
  
  <p>I have created a very simple service that will inject a stylesheet onto the page, 
  see the example code below:</p>
  
  <pre><code>var OfficeSuiteStylesheetInjectorModule = angular.module('OfficeSuiteStylesheetInjectorModule', []);
  
  OfficeSuiteStylesheetInjectorModule.factory('stylesheetInjectorService', ['$q', function($q) {
      // Returns the service itself.
      return {
          inject: function(id, uri) {
              var deferred = $q.defer();
  
              // Embed the stylesheet into the page, but only when it's non-existing.
              if (!angular.element('#' + id).length) {
                      var link = StylesheetFactory.Create(id, uri);
                      {
                          link.onload = deferred.resolve;
                          angular.element('head').append(link);
                      }
  
                      return deferred.promise;
                  }
          }
      }
  }]);
  </code></pre>
  
  <p>It's not a big service, it's just dependend on <code>$q</code> for promises so that I can run additional logic when the stylesheet has been embedded in the page.</p>
  
  <p>Now, I'm using Jasmine (I'm quite new to this) for testing my JavaScript code and I would like to test this module.</p>
  
  <p>I have a skeleton:</p>
  
  <pre><code>// Tests for the angular 'StylesheetInjectorService'.
  describe('StylesheetInjectorService', function() {
      var stylesheetInjectorService = {};
  
      // This code is executed before every test.
      beforeEach(function() {
          // Tells jamine that the module we're working on is the 'OfficeSuiteStylesheetInjectorModule'.
          angular.module('OfficeSuiteStylesheetInjectorModule');
      });
  
      // Ensures that it injects a stylesheet element into the page.
      it('Should inject a stylesheet element into the page.', function() {
          // How to test here that the stylesheet is injected?
      });
    });
  });
  </code></pre>
  
  <p>How can I inject te service in the page and ensures that the stylesheet is loaded?</p>
  
  <p><strong>Edit: Loading service now works:</strong></p>
  
  <pre><code>beforeEach(module('OfficeSuiteStylesheetInjectorModule'));
  
  // This code is executed before every test.
  beforeEach(function() {
  
      // Inject the required dependencies into the page.
      inject(function($injector) {
          stylesheetInjectorService = $injector.get('stylesheetInjectorService');
      });
  });
  </code></pre>
  
  <p>The same question is still open however. How to test if a stylesheet was embedded in the page?</p>
  
  <p>Any help is highly appreciated.</p>
  
  <p>Kind regards</p>