faizanjehangir
faizanjehangir

Reputation: 2851

TypeError: undefined is not a function

I am testing my Service using httpBackend. It is a simple get request that I want tested (response code) in this case. But I keep on getting the error:

TypeError: undefined is not a function (evaluating '$httpBackend.expectGET('/api/something').response({})') in test/spec/services/requests.js (line 22)
        test/spec/services/requests.js:22:51
        Error: Unsatisfied requests: GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1812)
        verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1812:74
        /test/spec/services/requests.js:28:48

This is the service:

angular.module('requestService', [])
  .factory('Request', ['$http', 'localConfig', 'Query', function($http, localConfig){
return {
      getRequest: function(fromDate, toDate) {
        return $http({
          url: '/api/something',
          method: "GET",
          params: {fromDate: fromDate,
                    toDate: toDate},
          headers: {
            'Content-Type': 'application/json; charset=utf-8'
          }
        })
      }
     }
  }]);

This is how I am testing it:

describe('Service: Request', function () {
  var reqService, $httpBackend;
  beforeEach(module('webapp'));
  beforeEach(module('requestService'));
  beforeEach(inject(function(Request, _$httpBackend_){
    reqService = Request;
    $httpBackend = _$httpBackend_;
  }));

  it('should test the response to be 200', function(){
    $httpBackend.expectGET('/api/something').response({});
    $httpBackend.flush();
  });

  afterEach(function(){
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  })
});

EDIT

I made the change(as suggested) to my test:

it('should test the response to be 200', function(){
    console.log('service: ' + reqService);
    reqService.getRequest('2016-03-26', '2016-03-29');
    $httpBackend.expectGET('/api/something').respond({});
    $httpBackend.flush();
  });

Now I am getting this error:

 Error: Unexpected request: GET /api/something?fromDate=2016-03-26&toDate=2016-03-29
        Expected GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1403)
        $httpBackend@/bower_components/angular-mocks/angular-mocks.js:1403:90
        sendReq@/bower_components/angular/angular.js:11235:21
        serverRequest@/bower_components/angular/angular.js:10945:23
        processQueue@/bower_components/angular/angular.js:15552:30
        /bower_components/angular/angular.js:15568:39
        $eval@/bower_components/angular/angular.js:16820:28
        $digest@/bower_components/angular/angular.js:16636:36
        flush@/bower_components/angular-mocks/angular-mocks.js:1778:45
        /test/spec/services/requests.js:24:23
        Error: [$rootScope:inprog] $digest already in progress
        http://errors.angularjs.org/1.5.0/$rootScope/inprog?p0=%24digest in bower_components/angular/angular.js (line 17178)
        beginPhase@/bower_components/angular/angular.js:17178:88
        $digest@//bower_components/angular/angular.js:16616:19
        verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1810:45
        /test/spec/services/requests.js:29:48

Upvotes: 0

Views: 522

Answers (2)

alecxe
alecxe

Reputation: 474221

I think you meant to use respond(), not response():

$httpBackend.expectGET('/api/something').respond({});

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

You haven't called getRequest function of service, so ajax call never get called & that's whyexpectGET isn't got satiesfied & thrown Error: Unsatisfied requests: Error. Also fix typo of response to respond

it('should test the response to be 200', function(){
    //action
    reqService.getRequest();

    $httpBackend.expectGET('/api/something').respond({});
    $httpBackend.flush();
});

Upvotes: 1

Related Questions