Unexpected request: GET No more request expected at $httpBackend
Asked Answered
T

1

19

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?

Tetraspore answered 4/11, 2013 at 23:51 Comment(0)
A
13

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.

Adytum answered 9/1, 2014 at 18:7 Comment(5)
templates then cause this failure as well.Howlond
Do you think that using $templateCache like ui.bootstrap does would nullify the need of mocking the GET?Billingsley
@Billingsley Can you post a link to where that is used?Adytum
@Billingsley if you look closer, they used inline HTML elements as opposed to load templates from a real file.Garnes
@Garnes "jsified" templates to be precise, with grunt-html2js pluginBillingsley

© 2022 - 2024 — McMap. All rights reserved.