0

I've mocked an httpBackend to test the following code.

  self.Api.post('/endpoint/action/', actionData)
    .then(function(resp){
      result = _.get(resp, 'data.MessageList');
      if(resp.status = 200 && result) {
        for (var i = 0; i < result.length; i++) {
          addResultMessage(result[i]);
        }
      }
    });

I mocked my service doing the following:

  beforeEach(function(){
      angular.mock.module(function(ApiProvider) {
        ApiProvider.setUrl('http://localhost:10010');
      });
  });

  beforeEach(inject(function($rootScope, $controller, _$interval_,  $q, $injector, $httpBackend){
    $rootScope = $injector.get('$rootScope');
    scope = $rootScope;
    httpBackend = $httpBackend;

    createController = function() {
      return $controller('MyCtrl',{
        $scope: scope,
        $uibModalInstance: modalInstance,
        $interval: _$interval_,
      });
    }
  }));

Inside an it

       httpBackend
        .when('POST', 'http://localhost:10010/endpoint/action/')
          .respond(200, mockedResponse);

I'm not getting any response when I do the POST to that endpoint. I don't get any error simply I don't get any resp callback

acostela
  • 2,597
  • 3
  • 33
  • 50

1 Answers1

0

You should call flush() after the test, like so:

it('should read expired login data', inject(function(authSrvc) {
    $httpBackend.expectGET('data/auth.json').respond(200, mockedResponse);

    authSrvc.checkStatus().then(function(data){
      expect(authSrvc.isValidSession()).toEqual(false);
    });

    $httpBackend.flush();
  }));
Emanuel Ralha
  • 754
  • 4
  • 12