意外请求:GET $httpBackend 不再有请求 [英] Unexpected request: GET No more request expected at $httpBackend

查看:21
本文介绍了意外请求:GET $httpBackend 不再有请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的作用域中有一个函数可以在用户单击按钮时检索我的服务状态,或者当某个事件被触发并自动调用该函数时.

I have a function in my scope to retrieve the status of my service when the user clicks a button, or when some event are triggered and this function is automatically called.

这是我的函数,在我使用的控制器中定义:

This is my function, defined in the controller I am using:

$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}});

  });

});

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

In the unit test I have also other $httpBackend tests on the same controller, but all of them works just somoothely. What am I doing wrong?

推荐答案

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

You need to supply the whenGET before you call the method.

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();
});

设置请求的期望,然后触发请求.

Set up the expectation of the request then trigger the request.

希望这会有所帮助.

这篇关于意外请求:GET $httpBackend 不再有请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆