gpt4 book ai didi

angularjs - 使用 $httpBackend 单元测试 AngularJS 给出 "Error: Unexpected Request"

转载 作者:行者123 更新时间:2023-12-04 15:23:22 25 4
gpt4 key购买 nike

我将在这个问题的开头说我已经得到了一个测试来为我的“LoginCtrl”工作,它执行几乎完全相同的任务。我确定这是一个我忽略的非常小的问题。在此先感谢您的帮助:)

我已经为 httpBackend 设置了预期值/响应用于我的测试,但是当我运行时 httpBackend.flush() ,就好像我从来没有设定过期望。

测试代码:

describe('user register', function () {
var scope, RegisterCtrl, httpBackend, userJohnDoe, userJohnDoeRegistered, Restangular;


// TEST
beforeEach(module('LR.User.Register'));


describe('RegisterCtrl', function () {
beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, _Restangular_, $state) {
scope = $rootScope.$new();
httpBackend = _$httpBackend_;
Restangular = _Restangular_;

// Models
userJohnDoeRegistered = {
"access_token": "YzEyZDhjNjBlY2EwMTMxMmQzZGIzYWY5NDY4OGYwMjMzMGVjNDU3MDVhY2U0YjJmNDc1ODI3NWU0ODkzZGNkMQ",
"expires_in": 7200,
"token_type": "bearer",
"scope": null,
"refresh_token": "MmFhMTQ5NzU4ODI5ZjE2Mjk3ZjNlYzEwYzJkMjc4M2NkZjY2MjVkMjIwNWQzODUxYWNiYzY3NzIyMzEwYzJhNg",
"user": {
"username": "johndoe",
"email": "johndoe@test.net",
"id": 1,
"first_name": "John",
"middle_name": "f",
"last_name": "Doe",
"created": "2013-09-04T10:46:10-0500",
"updated": "2013-09-04T10:46:10-0500"
},
"status": "success",
"status_code": 200,
"status_text": "OK"
};
userJohnDoe = {
"email": "johndoe@test.net",
"first_name": "John",
"middle_name": "f",
"last_name": "Doe",
password: 'test123X',
passwordSecond: 'test123X',
client_id: '1_3b5zgimwg4kkko4wksk4sw0o48040o8ws8og8kg4wsowwkc44s'
};

httpBackend.whenPOST('/register', userJohnDoe).respond(userJohnDoeRegistered);


RegisterCtrl = $controller('RegisterCtrl', {$scope: scope, $state: $state, Restangular: Restangular});
}));
afterEach(function () {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});


it('should be able to register a fake user', inject(function () {
var resolvedRegistration;
var cb = function (response) {
// Expect the userJohnDoe object
resolvedRegistration = sanitizeRestangularOne(response);

console.log(logTestHeader('REGISTRATION RESPONSE') +
JSON.stringify(resolvedRegistration, null, ' '));
};

// Set credentials
scope.user = userJohnDoeCredentials;

// Call login
var register = scope.register_user().then(function() {
cb();
});
scope.$digest();
httpBackend.flush();
expect(resolvedRegistration).toEqual(userJohnDoeRegistered);
}));
});


});

输出响应:
Chrome 29.0.1547 (Mac OS X 10.8.4) user register RegisterCtrl should be able to register a fake user FAILED
Error: Unexpected request: POST /register
No more request expected
at Error (<anonymous>)
at $httpBackend (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular-mocks/angular-mocks.js:959:9)
at y (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:64:282)
at g (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:62:272)
at r (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:96:140)
at r (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:96:140)
at /Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:97:293
at Object.g.$eval (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:104:502)
at Object.g.$digest (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular/angular.min.js:102:419)
at null.<anonymous> (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/src/app/user/register/register.spec.js:74:19)
Error: Declaration Location
at window.jasmine.window.inject.angular.mock.inject (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/vendor/angular-mocks/angular-mocks.js:1781:25)
at null.<anonymous> (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/src/app/user/register/register.spec.js:57:54)
at null.<anonymous> (/Users/bobothefrofro/Development/lr-phoenix/phoenix-client/src/app/user/register/register.spec.js:9:5)
at /Users/bobothefrofro/Development/lr-phoenix/phoenix-client/src/app/user/register/register.spec.js:1:1

最佳答案

我的第一个想法是当你实例化你的 Controller 时有什么东西在发出请求。也许尝试在您的 Controller 声明行之后添加 $httpBackend.flush() ,看看是否可以解决它。另外,您的 $scope.$digest 应该在 $httpBackend.flush 之后而不是之前吗?

关于angularjs - 使用 $httpBackend 单元测试 AngularJS 给出 "Error: Unexpected Request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619432/

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