Angularjs 没有在函数处刷新的挂起请求。$httpBackend.flush
我是Angular JS的新手,尝试使用karma jasmine实现单元测试。此错误Angularjs 没有在函数处刷新的挂起请求。$httpBackend.flush,angularjs,Angularjs,我是Angular JS的新手,尝试使用karma jasmine实现单元测试。此错误 已经记录了很多次,这些解决方案对我没有帮助(可能是我做错了什么)。以下代码始终不允许任何刷新请求。如果我注释$httpBackend.Flush,它将在$httpBackend.verifyNoOutstandingExpectation()处中断,错误为未满足请求。 有人能帮我找出下面代码的问题吗 UnitTest Code beforeEach(inject(function(_$httpBacken
已经记录了很多次,这些解决方案对我没有帮助(可能是我做错了什么)。以下代码始终不允许任何刷新请求。如果我注释$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);
});
};