title: Reset service object after each test
body: <p>I'm trying to test my Auth service in my AngularJS app.</p>
  
  <p>This is the service:</p>
  
  <pre><code>'use strict';
  
  angular.module('testApp')
      .factory('Auth', function ($window, $http, $location, $q) {
          var currentUser;
  
          return {
              authenticate: function (email, password) {
                  //promise to return
                  var deferred = $q.defer();
  
                  var authRequest = $http.post('https://' + $location.host() + ':3005/authenticate', {email: email, password: password});
  
                  authRequest.success(function (data, status, header, config) {
                      //Store currentUser in sessionStorage
                      currentUser = data;
                      $window.sessionStorage.setItem('currentUser', JSON.stringify(currentUser));
                      //resolve promise
                      deferred.resolve();
                  });
  
                  authRequest.error(function (data, status, header, config) {
                      //reject promise
                      deferred.reject('Invalid credentials.');
                  });
  
                  return deferred.promise;
              },
              isAuthenticated: function () {
                  return this.getCurrentUser() !== null;
              },
              getCurrentUser: function () {
                  if (currentUser !== undefined) {
                      return currentUser;
                  } else {
                      currentUser = JSON.parse($window.sessionStorage.getItem('currentUser'));
                      return currentUser;
                  }
              },
              logOut: function () {
                  var that = this;
                  $http.get('https://' + $location.host() + ':3005/logout')
                      .success(function (data, status, header, config) {
                          that.appLogOut();
                          $location.path('/login');
                      }).
                      error(function (data, status, headers, config) {
                          console.log('logout error');
                      });
              },
              appLogOut: function () {
                  console.log('appside log out');
                  currentUser = null;
                  $window.sessionStorage.removeItem('currentUser');
              }
          };
      });
  </code></pre>
  
  <p>And this is my test:</p>
  
  <pre><code>'use strict';
  
  describe('Service: Auth', function () {
  
      // load the service's module
      beforeEach(module('testApp'));
  
      // instantiate service and any mock objects
      var Auth,
          httpBackend;
  
      //http://code.angularjs.org/1.2.14/docs/api/ngMock/function/angular.mock.inject
      beforeEach(inject(function (_Auth_, $httpBackend) {
          Auth = _Auth_;
          httpBackend = $httpBackend;
      }));
  
      // verify that no expectations were missed in the tests
      afterEach(function () {
          httpBackend.verifyNoOutstandingExpectation();
          httpBackend.verifyNoOutstandingRequest();
      });
  
      it('should be instantiated', function () {
          (!!Auth).should.be.true;
      });
  
      describe('authenticate(email, password)', function () {
          var user = {
              email: 'shaun@test.com',
              password: 'password',
              sessionId: 'abc123'
          };
  
          it('should make a call to the server to log the user in - and FULFILL promise if response == 200', function () {
              httpBackend.whenPOST(/https:\/\/.+\/authenticate/, {
                  email: user.email,
                  password: user.password
              }).respond(200, user);
  
              var promise = Auth.authenticate(user.email, user.password);
  
              httpBackend.flush();
  
              promise.should.eventually.be.fulfilled;
          });    
      });
  
  
      describe('isAuthenticated()', function () {
          it('should return false if user is not authenticated', function () {
              Auth.isAuthenticated().should.be.false;
          });
      });
  
      describe('logOut()', function () {
          it('should make a call to the server to log the user out', function () {
              // expect a GET request to be made
              // regex to capture all requests to a certain endpoint regardless of domain.
              httpBackend.expectGET(/https:\/\/.+\/logout/).respond(200);
  
              // call the logOut method on Auth service
              Auth.logOut();
  
              // flush to execute defined mock behavior.
              httpBackend.flush();
          });
      });
  
  });
  </code></pre>
  
  <p>My problem is with the following test:</p>
  
  <pre><code>describe('isAuthenticated()', function () {
      it('should return false if user is not authenticated', function () {
          Auth.isAuthenticated().should.be.false;
      });
  });
  </code></pre>
  
  <p>From what I understand, each 'describe' and/or 'it' block should be completely independent. I would think that a fresh instance of 'Auth' is injected before each test. However, the above test is failing because of the successful authentication test before this test is running.</p>
  
  <p>Hence the output becomes:</p>
  
  <pre><code>Chrome 33.0.1750 (Mac OS X 10.8.2) Service: Auth isAuthenticated() should return false if user is not authenticated FAILED
      expected true to be false
      AssertionError: expected true to be false
  </code></pre>
  
  <p>What am I missing? Do I have to manually reset the Auth object after each test? I tried setting Auth = {} in an afterEach() function but that didn't seem to change anything.</p>
  
  <p>Thanks for taking the time to read this question.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I know the problem. In Auth.getCurrentUser(), I am grabbing the 'currentUser' from $window.sessionStorage. So, I do get a new instance of Auth with each test (I think), but the same instance of $window.sessionStorage is being used.</p>
  
  <p>Question should now be .. 'How do I clear $window.sessionStorage' after each test.</p>