mardi 29 mars 2016

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

Aucun commentaire:

Enregistrer un commentaire