Error: Unexpected request: POST Karma-jasmine-angular
Error: Unexpected request: POST Karma-jasmine-angular
实际情况就像我尝试一起测试我的 angular js 控制器和服务时,首先我调用 get ajax 请求成功后,我调用了一个用户服务,其中还包含一个 post ajax 请求。当我 运行 测试时,出现以下错误。
Error: Unexpected request: POST http://localhost:8080/services/access
No more request expected in
/home/test/WebstormProjects/test2/bower_components/angular-mocks/angular-mocks.js
我在堆栈溢出中发现了一些类似的问题,但它不适合我的场景。这些是我发现的以下问题。
这是我尝试过的代码。
test.js
describe('Login controller',function(){
beforeEach(module('app'));
var httpBackend,ctrl, scope,userService;
beforeEach(inject(function($controller, $rootScope,$httpBackend,$injector,_userService_) {
scope = $rootScope.$new();
httpBackend = $injector.get('$httpBackend');
userService = _userService_;
ctrl = $controller('LoginController', {
$scope: scope
});
}));
it('should set authentication true', function() {
scope.authenticateUser();
var username = 'test';
var password = '123456';
var requestToken = "";
httpBackend.whenGET('http://localhost:8080/services/request').respond(200,{
status : 0
});
httpBackend.expect('POST', 'http://localhost:8080/services/access').respond(200, {
status:"success"
});
var returnedPromise = userService.authenticateUser(username,password,requestToken);
httpBackend.flush();
expect(scope.result).toEqual(0);
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
});
app.js
app.controller('LoginController', function LoginController($scope,$http,userService) {
$scope.test="hello";
$scope.authenticateUser = function(){
var username = 'test';
var password = '123456';
var token = "";
$http.get('http://localhost:8080/services/request').success(function(data) {
$scope.result = data.status;
userService.authenticateUser(username, password, "").then(function(response){});
});
};
});
userService.js
userService.authenticateUser = function(username, password,requestToken){
var status = $http({
method: 'POST',
url: "http://localhost:8080/services/access",
data: $.param({username:username,password:password,request_token:requestToken}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
return data;
}).
error(function(data, status, headers, config) {
return null;
});
return status;
};
return userService;
我是 karma-jasmine-angular 测试的新手,我正在努力应对这种情况。请给我提供适当的指导来解决这种测试情况。任何帮助将不胜感激。如果可能的话,请给我一些好的演示来测试需要服务成功的服务。提前致谢。
当 POST
请求 http://localhost:8080/services/access
时,您忘记伪造后端响应。在测试中,您只测试预期的 POST
请求是否已发出或使用正确的 params
。只需添加假响应即可完美运行
httpBackend
.when('POST', 'http://localhost:8080/services/access')
.respond(200, {
status: "success"
});
Link fiddle 供您选择。
实际情况就像我尝试一起测试我的 angular js 控制器和服务时,首先我调用 get ajax 请求成功后,我调用了一个用户服务,其中还包含一个 post ajax 请求。当我 运行 测试时,出现以下错误。
Error: Unexpected request: POST http://localhost:8080/services/access No more request expected in /home/test/WebstormProjects/test2/bower_components/angular-mocks/angular-mocks.js
我在堆栈溢出中发现了一些类似的问题,但它不适合我的场景。这些是我发现的以下问题。
这是我尝试过的代码。
test.js
describe('Login controller',function(){
beforeEach(module('app'));
var httpBackend,ctrl, scope,userService;
beforeEach(inject(function($controller, $rootScope,$httpBackend,$injector,_userService_) {
scope = $rootScope.$new();
httpBackend = $injector.get('$httpBackend');
userService = _userService_;
ctrl = $controller('LoginController', {
$scope: scope
});
}));
it('should set authentication true', function() {
scope.authenticateUser();
var username = 'test';
var password = '123456';
var requestToken = "";
httpBackend.whenGET('http://localhost:8080/services/request').respond(200,{
status : 0
});
httpBackend.expect('POST', 'http://localhost:8080/services/access').respond(200, {
status:"success"
});
var returnedPromise = userService.authenticateUser(username,password,requestToken);
httpBackend.flush();
expect(scope.result).toEqual(0);
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
});
app.js
app.controller('LoginController', function LoginController($scope,$http,userService) {
$scope.test="hello";
$scope.authenticateUser = function(){
var username = 'test';
var password = '123456';
var token = "";
$http.get('http://localhost:8080/services/request').success(function(data) {
$scope.result = data.status;
userService.authenticateUser(username, password, "").then(function(response){});
});
};
});
userService.js
userService.authenticateUser = function(username, password,requestToken){
var status = $http({
method: 'POST',
url: "http://localhost:8080/services/access",
data: $.param({username:username,password:password,request_token:requestToken}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
return data;
}).
error(function(data, status, headers, config) {
return null;
});
return status;
};
return userService;
我是 karma-jasmine-angular 测试的新手,我正在努力应对这种情况。请给我提供适当的指导来解决这种测试情况。任何帮助将不胜感激。如果可能的话,请给我一些好的演示来测试需要服务成功的服务。提前致谢。
当 POST
请求 http://localhost:8080/services/access
时,您忘记伪造后端响应。在测试中,您只测试预期的 POST
请求是否已发出或使用正确的 params
。只需添加假响应即可完美运行
httpBackend
.when('POST', 'http://localhost:8080/services/access')
.respond(200, {
status: "success"
});
Link fiddle 供您选择。