title: How do I mock an Angular service using jasmine?
body: <p>This may be a duplicate but I have looked at a lot of other questions here and they usually miss what I am looking for in some way. They mostly talk about a service they created themselves. That I can do and have done. I am trying to override what angular is injecting with my mock. I thought it would be the same but for some reason when I step through the code it is always the angular $cookieStore and not my mock.</p>
  
  <p>I have very limited experience with jasmine and angularjs. I come from a C# background. I usually write unit tests moq (mocking framework for C#). I am use to seeing something like this</p>
  
  <pre><code>[TestClass]
  public PageControllerTests
  {
      private Mock&lt;ICookieStore&gt; mockCookieStore;
      private PageController controller;
  
      [TestInitialize]
      public void SetUp()
      {
          mockCookieStore = new Mock&lt;ICookieStore&gt;();
          controller = new PageController(mockCookieStore.Object);
      }
  
      [TestMethod]
      public void GetsCarsFromCookieStore()
      {
          // Arrange
          mockCookieStore.Setup(cs =&gt; cs.Get("cars"))
                      .Return(0);
  
          // Act
          controller.SomeMethod();
  
          // Assert
          mockCookieStore.VerifyAll();
      }
  }
  </code></pre>
  
  <p>I want mock the $cookieStore service which I use in one of my controllers.</p>
  
  <pre><code>app.controller('PageController', ['$scope', '$cookieStore', function($scope, $cookieStore) {
  
      $scope.cars = $cookieStore.get('cars');
  
      if($scope.cars == 0) {
              // Do other logic here
              .
      }
  
      $scope.foo = function() {
          .
          .
      }
  }]);
  </code></pre>
  
  <p>I want to make sure that the $cookieStore.get method is invoked with a 'garage' argument. I also want to be able to control what it gives back. I want it to give back 0 and then my controller must do some other logic.</p>
  
  <p>Here is my test.</p>
  
  <pre><code>describe('Controller: PageController', function () {
  
      var controller,
          scope,
          cookieStoreSpy;
  
      beforeEach(function () {
          cookieStoreSpy = jasmine.createSpyObj('CookieStore', ['get']);
          cookieStoreSpy.get.andReturn(function(key) {
              switch (key) {
                  case 'cars':
                      return 0;
                  case 'bikes':
                      return 1;
                  case 'garage':
                      return { cars: 0, bikes: 1 };
              }
          });
  
          module(function($provide) {
              $provide.value('$cookieStore', cookieStoreSpy);
          });
          module('App');
  
      });
  
      beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
          scope = $rootScope.$new();
          controller = $controller;
      }));
  
      it('Gets car from cookie', function () {
          controller('PageController', { $scope: scope });
          expect(cookieStoreSpy.get).toHaveBeenCalledWith('cars');
      });
  });
  </code></pre>