2016-03-23 12 views
0

rota gidermek, yer değiştirme, birim testleri, karma-yasemin, değilseniz sizeangularjs i bütün eylemler bir rota kararlılık yazdım açısal olarak

$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); 
        } 
       ); 
      }); 
     } 
    } 
}); 

kaydedilir veya değil Hava kontrol etmek giriş yaptık giriş sayfasına doğru yönlendiriyoruz. Şimdi bunu karma-yasemin birim testimizde test etmek istiyorum. Ancak testi yazarsam, location.path değişmez.

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'); 
     }); 
    }); 

}); 

Ama sonucudur:

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'. 

Şimdi ben casusu bazı belgeleri gördüm, ama bu konum değişikliği için nasıl kontrol edileceğini alamadım.

cevap

0

Herbiri gerçekleşmeden önce bu sorunu kendiniz çözerek kendiniz test edebilirsiniz.

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'); 
     }); 
    }); 

});