意外的请求:拿不出更多的要求预计将在$ httpBackend [英] Unexpected request: GET No more request expected at $httpBackend

查看:130
本文介绍了意外的请求:拿不出更多的要求预计将在$ 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.';
    });
  }

单元测试是由这样的:

The unit test is made like:

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同一个控制器上的测试,但他们都工作得somoothely。我在做什么错了?

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.

希望这有助于。

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

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