2015-01-24 4 views
1

Я новичок в модульного тестирования, и я пытаюсь писать тесты для моего LoginController:AngularJS жасмин написания кода тестирования

function loginController($scope, $state, authService) { 
    $scope.loginData = { 
     userName: "", 
     password: "" 
    }; 
    $scope.message = ""; 

    $scope.login = function() { 
     authService.login($scope.loginData).then(
      function (response) {}, 
      function (err) { 
       $scope.message = err.error_description; 
      }); 
     }; 

//-----Load---------------------------------------- 

    if (authService.authentication.isAuth) { 
     if ($scope.$stateChangeStart != null && $scope.$stateChangeStart.length > 0) { 
       //$scope.message = "testing2"; 
       $state.transitionTo($scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toState, $scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toParams); 
      } else { 
       // $scope.message = "testing"; 
       $state.transitionTo('home'); 
      } 
     } 
    } 
})(); 

Я пытаюсь проверить код загрузки, если пользователь Войти перенаправлять на последний знать состояние. Я получаю неудачу на последних expect($state.transitionTo).toHaveBeenCalledWith($scope.$stateChangeStart[$scope.$stateChangeStart.length - 2].toState, $scope.$stateChangeStart[$scope.$stateChangeStart.length - 2].toParams);.

Это не имеет смысла для меня. Когда я раскомментирую expect($state.transitionTo).toHaveBeenCalledWith('home'); и прокомментирую последние expect($state.transitionTo).toHaveBeenCalledWith($scope.$stateChangeStart[$scope.$stateChangeStart.length - 2].toState, $scope.$stateChangeStart[$scope.$stateChangeStart.length - 2].toParams);, тогда тест прошел. Любая идея почему?

Вот мой тест:

it('should redirect to last state when login in', function() { 
     setAuthentication(); 
     spyOn($state, 'transitionTo').andCallThrough(); 
     var controller = createController(); 
     $httpBackend.flush(); 

     expect($authService.authentication.isAuth).toBe(true); 
     expect($scope.$stateChangeStart).not.toBe(null); 
     expect($scope.$stateChangeStart.length > 0).toBeTruthy(); 
     //expect($state.transitionTo).toHaveBeenCalledWith('home'); 
     expect($state.transitionTo).toHaveBeenCalledWith($scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toState, $scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toParams); 
    }); 

ответ

1

Я должен очистить изменения authService:

Этот тест проходят сейчас:

it('should redirect to last state when login in', function() { 
    setAuthentication(); 
    $httpBackend.flush(); 
    spyOn($state, 'transitionTo').andCallThrough(); 
    var controller = createController(); 

    expect($authService.authentication.isAuth).toBe(true); 
    expect($scope.$stateChangeStart).not.toBe(null); 
    expect($scope.$stateChangeStart.length > 0).toBeTruthy(); 
    //expect($state.transitionTo).toHaveBeenCalledWith('home'); 
    expect($state.transitionTo).toHaveBeenCalledWith($scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toState, $scope.$stateChangeStart[$scope.$stateChangeStart.length - 1].toParams); 
}); 
Смежные вопросы