Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

angularjs - Unexpected request: GET No more request expected at $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}});

  });

});

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...