angularjs – 意外请求:GET在$httpBackend期间不再需要其他请求
发布时间:2020-12-17 08:10:39 所属栏目:安全 来源:网络整理
导读:在我的范围内,我有一个功能来检索我的服务的状态,当用户点击一个按钮,或者 当某些事件被触发并且该功能被自动调用时。 这是我的功能,在我使用的控制器中定义: $scope.getStatus = function() { $http({method: 'GET',url: config.entrypoint + config.a
在我的范围内,我有一个功能来检索我的服务的状态,当用户点击一个按钮,或者
当某些事件被触发并且该功能被自动调用时。 这是我的功能,在我使用的控制器中定义: $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,"errors":null}}); }); }); 在单元测试中,我还在同一个控制器上进行了其他$ httpBackend测试,但是所有这些测试都只能轻松工作。我究竟做错了什么?
在调用方法之前,需要提供whenGET。
it('Should get the status',function() { scope.serviceId = '09bb5943fa2881e1'; $httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200,"errors":null}}); scope.getStatus(); }); 设置请求的期望,然后触发请求。 希望这可以帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |