没有待处理的函数刷新请求。$ httpBackend.flush

时间:2015-06-26 13:23:43

标签: angularjs

我是Angular JS的新手并尝试使用karma-jasmine实现单元测试。这个错误
已被记录多次,这些解决方案没有帮助我(可能是我做错了什么)。以下代码总是没有请求刷新。如果我评论$ httpBackend.Flush,它会在$ httpBackend.verifyNoOutstandingExpectation()处中断,但错误的未满足请求。     有人可以帮我找出下面代码的问题吗?

UnitTest Code

 beforeEach(inject(function(_$httpBackend_, _$rootScope_,  _$controller_, _$http_) {    
     *// Set up the mock http service responses*    
     $httpBackend = _$httpBackend_;    
     http = _$http_;    
     *//mock data to be returned*    
     var mockData = {"role": "DEBTOR11", "PSPID": "ff45RT45RTYHH6", "accessToken":{"token":"1234","expire":"2"}};    
     $httpBackend.when('POST', 'http://172.16.5.172:8080/api/authenticate/login')
                            .respond(function(){ return (200,mockData); });       
     *// Get hold of a scope (i.e. the root scope)*  
     $rootScope = _$rootScope_;  
     *// The $controller service is used to create instances of controllers*  
     controller =  _$controller_;  
     scope = $rootScope.$new();    

   }));

 it('should fetch authentication token', function() {  
     CTRL = controller('loginCtrl', {$scope : $rootScope });  
     scope.email = 'abc@gmail.com';  
     scope.password = '1234';  
     $httpBackend.expectPOST('http://172.16.5.172:8080/api/authenticate/login', {email:scope.email, password:scope.password})
                 .respond(function(){ return (200,mockData); });  
    scope.loginbtnEvent();  
     $httpBackend.flush();  
     expect(scope.Token).not.toBe(null);  
     });

CONTROLLER CODE:

$scope.loginbtnEvent = function() {  
          //API call for login  
          alert('landed in login button event');  
          $http.post('http://172.16.5.172:8080/api/authenticate/login', {email:$scope.email, password:$scope.password}).  
          success(function(data, status, headers, config) {alert('landed in success button event');  
              //on success data will contain TOKEN as shown in the commented section
              $scope.token = data;
              //set the token in the local storage  
              localStorageService.set(CACHE_LOGIN_TOKEN,$scope.token);  
              //set the login token created date-timestamp in the local storage  
              localStorageService.set(CACHE_LOGIN_TOKENDATE,new Date());  
              //redirect to inner page  
              window.location.href = FILE_PATH_TEMPLATE;
              $scope.Message = "Success";  
          }).
          error(function(data, status, headers, config) {  
              $scope.Message = "Success";//"Invalid login credentials. Please check username / password.";  
              $scope.email = "";  
              $scope.password = "";  
              localStorageService.remove(CACHE_LOGIN_TOKEN);  
          });

      };

0 个答案:

没有答案