gpt4 book ai didi

angularjs - 意外请求 : GET No more request expected at $httpBackend

转载 作者:行者123 更新时间:2023-12-03 12:57:03 25 4
gpt4 key购买 nike

我的范围内有一个函数可以在用户单击按钮时检索我的服务状态,或者
当某些事件被触发并自动调用此函数时。

这是我的功能,在我使用的 Controller 中定义:

$scope.getStatus = function() {
$http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId})
.success(function(data) {
$scope.errorMessage = '';
$scope.serviceAllGood = data;
})
.error(function() {
$scope.serviceAllGood = '';
$scope.errorMessage = 'We are experiencing problems retrieving your service status.';
});
}

单元测试如下:
describe('SendServiceCtrl', function(){
var scope, ctrl, $httpBackend, configuration;

beforeEach(function () {
module('papi', 'ngUpload');
});

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) {
configuration = config;
$httpBackend = _$httpBackend_;
$httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}});
scope = $rootScope.$new();
ctrl = $controller('SendServiceCtrl', {$scope: scope});
}));

it('Should get the status', function() {

scope.serviceId = '09bb5943fa2881e1';
scope.getStatus();
$httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}});

});

});

在单元测试中,我还在同一个 Controller 上进行了其他 $httpBackend 测试,但它们都运行得非常顺利。我究竟做错了什么?

最佳答案

您需要提供 whenGET在调用该方法之前。

it('Should get the status', function() {
scope.serviceId = '09bb5943fa2881e1';
$httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}});
scope.getStatus();
});

设置请求的期望然后触发请求。

希望这可以帮助。

关于angularjs - 意外请求 : GET No more request expected at $httpBackend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779528/

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