Unexpected request: GET No more request expected at $httpBackend

Mimo picture Mimo · Nov 5, 2013 · Viewed 24k times · Source

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

  });

});

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?

Answer

km6zla picture km6zla · Jan 9, 2014

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.

Hope this helps.