gpt4 book ai didi

angularjs - 错误 : Unexpected request: POST Karma-jasmine-angular

转载 作者:行者123 更新时间:2023-12-05 01:00:10 30 4
gpt4 key购买 nike

实际情况就像当我尝试一起测试我的 angular js Controller 和服务时,首先我调用 get ajax 请求,成功后我调用用户服务,其中还包含 post _0x10456792 请求。当我运行测试时,出现以下错误。

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



我在堆栈溢出中发现了一些类似的问题,但它不适合我的情况。这些是我发现的以下问题。

Unexpected request: GET No more request expected at $httpBackend

Mocking $httpBackend - how to handle "Unexpected request, No more request expected"?

AngularJS $httpBackend - "No more request expected" error



这是我所做并尝试的代码。

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根据您的喜好。

关于angularjs - 错误 : Unexpected request: POST Karma-jasmine-angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29937261/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com