title: Jasmin-Karma and mocking localStorage spyOn could not find an object to spy upon for getItem()
body: <p>I am currently evolving into TDD and want to test my <code>AuthController</code>. Inside my controller there is a service being injected, called <code>AuthService</code>. This service uses $localStorage, respectively the <code>ngstorage</code> bower package. I want to mock it in order to test my controller. I did so by using <code>SpyOn</code> provided by <code>jasmine</code>. As browser I use phantomjs. Unfortunately I am getting this error message by jasmine-core:</p>
  
  <pre><code>Error: spyOn could not find an object to spy upon for getItem()
  </code></pre>
  
  <p><strong>auth.controller.spec.coffee</strong>:</p>
  
  <pre><code>describe 'AuthController', -&gt;
  
    AuthController = undefined
    scope = undefined
    localStorage = undefined
  
    beforeEach -&gt;
      module('myApp')
      store = {}
      spyOn(localStorage, 'getItem').andCallFake (key) -&gt;
        store[key]
      spyOn(localStorage, 'setItem').andCallFake (key, value) -&gt;
        store[key] = value + ''
  
    beforeEach inject(($controller, $rootScope, _AuthService_) -&gt;
      scope = $rootScope.$new()
      AuthService = _AuthService_
      AuthController = $controller('AuthController', $scope: scope, AuthService:AuthService)
    )
  
    it 'verifies AuthService is injected', -&gt;
      expect(scope.AuthService).to.have.property 'email'
  
    it 'verifies signIn is working', -&gt;
      scope.AuthService.set('email', 'john@doe.com')
      expect(scope.AuthService.email) .to.equal 'john@doe.com'
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>