意外请求:GET $ httpBackend不再需要更多请求

Mim*_*imo 19 jasmine angularjs karma-runner httpbackend

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

这是我的功能,在我使用的控制器中定义:

$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.';
    });
  }
Run Code Online (Sandbox Code Playgroud)

单元测试如下:

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

  });

});
Run Code Online (Sandbox Code Playgroud)

在单元测试中,我在同一个控制器上也有其他的$ httpBackend测试,但所有这些测试都很有效.我究竟做错了什么?

km6*_*zla 13

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();
});
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.