2016-03-23 3 views
0

В Угловое я написал маршрутную решимость всех действий, чтобы проверить погоду вы вошли в систему или неAngularJS, маршрут Разрешая, изменение местоположения, модульное тестирование, карма-жасмин

$routeProvider.when('/home', { 
    resolve: { 
     Authenticated: function($location, AuthAuthenticator, AuthIdentity) { 
      return new Promise(function(resolve, reject) { 
       AuthAuthenticator.init().then(
        function(success) { 
         if (!AuthAuthenticator.isAuthenticated()) { 
          $location.path("/login"); 
          resolve(true); 
         } 
         resolve(false); 
        }, 
        function(error) { 
         reject(error); 
        } 
       ); 
      }); 
     } 
    } 
}); 

Если вы не вошедшем в систему, мы перенаправляем вас на страницу входа. Теперь я хочу проверить это в нашем карма-жасминовом модульном тесте. Но если я напишу тест, location.path не изменится.

describe('LoginController', function() { 
    beforeEach(module('dcApp')); 

    beforeEach(function() { 
     var _authenticated = false; 

     AuthAuthenticatorMock = { 
      isAuthenticated: function() { 
       return _authenticated 
      }, 

      setAuthenticated: function(authenticated) { 
       _authenticated = authenticated; 
      }, 
     }; 

     module(function($provide) { 
      $provide.value('AuthAuthenticator', AuthAuthenticatorMock); 
     }); 
    }); 

    var $controller; 

    beforeEach(inject(function(_$route_, _$location_, _$controller_, _AuthAuthenticator_){ 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $route = _$route_; 
     $location = _$location_; 
     $controller = _$controller_; 
     AuthAuthenticator = _AuthAuthenticator_; 
    })); 

    describe('Controller activation', function() { 
     it ('redirects to login if user is not yet logged in', function() { 
      AuthAuthenticator.setAuthenticated(false); 
      var $scope = {}; 
      var controller = $controller('HomeController', { $scope: $scope }); 
      expect($location.path()).toBe('/login'); 
     }); 
    }); 

}); 

Но результат:

PhantomJS 2.1.1 (Linux 0.0.0) HomeController Controller activation redirects to login if user is not yet logged in FAILED 
Expected '' to be '/login'. 

Теперь я видел некоторые документы на шпиона, но я не понимаю, как проверить это изменение местонахождения.

ответ

0

Вы можете проверить это правильно, вызвав решение самостоятельно после того, как произошли события до того, как они произошли.

describe('LoginController', function() { 
    beforeEach(module('dcApp')); 

    beforeEach(function() { 
     var _authenticated = false; 

     AuthAuthenticatorMock = { 
      isAuthenticated: function() { 
       return _authenticated 
      }, 

      setAuthenticated: function(authenticated) { 
       _authenticated = authenticated; 
      }, 
     }; 

     module(function($provide) { 
      $provide.value('AuthAuthenticator', AuthAuthenticatorMock); 
     }); 
    }); 

    beforeEach(function() { 
     var _path = ''; 

     locationMock = { 
      path: function(argument) { 
       if (argument) { 
        _path = argument; 
       } 
       return _path; 
      } 
     }; 

     module(function($provide) { 
      $provide.value('$location', locationMock); 
     }); 
    }); 

    var $controller; 

    beforeEach(inject(function(_$route_, _$location_, _$controller_, _AuthAuthenticator_){ 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $route = _$route_; 
     $location = _$location_; 
     $controller = _$controller_; 
     AuthAuthenticator = _AuthAuthenticator_; 
    })); 

    describe('Controller activation', function() { 
     it ('redirects to login if user is not yet logged in', function() { 
      AuthAuthenticator.setAuthenticated(false); 
      var resolveTest = $route.routes['/home'].resolve.Authenticated; 
      $injector.invoke(resolveTest); 
     // if it's a promise call apply 
      $rootScope.$apply(); 
      expect($location.path()).toBe('/login'); 
     }); 
    }); 

}); 
Смежные вопросы