title: Testing a AngularJS services with dependencies
body: <p>I have som issues testing a simple service in AngularJS, which depends on another service.</p>
  
  <p>The service looks a bit like this:</p>
  
  <pre><code>serviceModule.factory('filtersService', ['$rootScope', 'urlService', function($rootScope, urlService){
          return {
              getFilters: function(){
                     if(urlService.getPathName() == "test")
                     {
                        return "something";
                 }
      }]);
  </code></pre>
  
  <p>In the start I just tried to use window.location.pathname instead of creating a service, but it seemed like the wrong way to be able to mock it. I therefore created the urlService, which is basically a simple wrapper around the window-object.</p>
  
  <p>I then have my filtersServiceTest which looks like this: </p>
  
  <pre><code>describe('filtersService test', function(){
      var filtersService, urlServiceMock;
  
      beforeEach(module('App.services'));
  
      beforeEach(function(){
          urlServiceMock = {
              getPathName: function(){
                  return "";
              }
          };
  
          module(function($provide){
              $provide.value('urlService', urlServiceMock);
          });
      });
  
      it('Should return something if url is test', inject(function(filtersService){
          urlServiceMock = {
              getPathName: function(){
                  return "test";
              }
          };
  
          expect(filtersService.getFilters()).not.toBe("something");
      }));
  });
  </code></pre>
  
  <p>But this doesn't seem to work. I can't overload the urlServiceMock before it is actually run. I could change the '<code>beforeEach</code>' each so getPathName returns "test", but then I would not be able to test scenarious where the urls isn't equal to test.</p>