如何为angularjs代码编写单元测试

时间:2018-10-28 12:20:32

标签: angularjs

如何在茉莉花角JS中编写以下代码的单元测试用例。使用Karma Framwork。我无法为submit()函数中的$ hhtp()方法编写测试用例,以及如何取消测试submit()函数中的范围变量

$scope.submit = function(){
          var user = {email: $scope.user.email, password: $scope.user.password };
          $http({
            method: 'POST',
            url: api.host_url + "admin_sign_in",
            data: user
          }).then(function(data){
            data = data.data;
            if(data.success){
              $rootScope.is_login = true;
              localStorage.setItem("id", data.user.id);
              // service function to store authentication tokens into local storage
              manageTokens.storeTokens(data.authentication_token,data.refresh_token);
              localStorage.setItem('is_login', true);
              // checks to authorise user as per his role
              if(data.user.role === "Super Admin"){
                $rootScope.name = "Super Admin";
                localStorage.setItem('user_name',data.user.name);
                localStorage.setItem('role','admin');
                $state.go('admin-dashboard');
              }else if(data.user.role === 'Site Admin'){
                $rootScope.name = data.user.name;
                localStorage.setItem('user_name',data.user.name);
                localStorage.setItem('role','site admin');
                $state.go('admin-dashboard');
              }
              else if(data.user.role === 'Board Member'){
                // $rootScope.name = "Super Admin";
                localStorage.setItem('user_name',data.user.name);
                localStorage.setItem('role','Board Member');
                $state.go('user-dashboard',{site_id: data.site_id});
              }
            }else{
              if(!data.success){
                $scope.loginErrorMessage = data.message;
                $scope.showError = true;
              }
              $scope.errorMessage = "Not valid admin credentials!";
              $state.go('login');
            }
          }).catch(function(error){
            console.log(error);
          });
        };

0 个答案:

没有答案