How do I stub `$window.localStorage` in my AngularJS unit tests?
问题 angular.module('MyApp') .controller('loginCtrl', function($scope, $rootScope, $location, $window, $auth) { $scope.login = function() { $auth.login($scope.user) .then(function(response) { if(response.data.users.status === 'success'){ $rootScope.currentUser = response.data.username; $window.localStorage.user = JSON.stringify(response.data.users); $location.path('/'); } }).catch(function(response){ if(response.data.users.status === 'error'){ $scope.error = response.data.users.error; } }) }; });