lundi 28 septembre 2015

Evaluating $httpBackend.expectGet('url') results in 'undefined' is not a function error

I'm trying to write a unit test for a service using $httpBackend to mock http requests. For now I'm just trying to get expectGET to work but I get the following error:

TypeError: 'undefined' is not a function (evaluating 'encoder.encode.bind(encoder)')
    at FormEncoderService (/Users/photon/Development/3C/client/bower_components/stormpath-sdk-angularjs/dist/stormpath-sdk-angularjs.js:1473)
    at formEncoderServiceFactory (/Users/photon/Development/3C/client/bower_components/stormpath-sdk-angularjs/dist/stormpath-sdk-angularjs.js:1574)
    at invoke (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4476)
    at /Users/photon/Development/3C/client/bower_components/angular/angular.js:4293
    at getService (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4435)
    at invoke (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4467)
    at /Users/photon/Development/3C/client/bower_components/angular/angular.js:4293
    at getService (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4435)
    at invoke (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4467)
    at /Users/photon/Development/3C/client/bower_components/angular/angular.js:4297
    at forEach (/Users/photon/Development/3C/client/bower_components/angular/angular.js:336)
    at createInjector (/Users/photon/Development/3C/client/bower_components/angular/angular.js:4297)
    at workFn (/Users/photon/Development/3C/client/bower_components/angular-mocks/angular-mocks.js:2427)
    at /Users/photon/Development/3C/client/bower_components/angular-mocks/angular-mocks.js:2410
    at /Users/photon/Development/3C/client/test/spec/services/dataservice.js:17
TypeError: 'undefined' is not a function (evaluating '$httpBackend
      .expectGET('http:/localhost:3000/api/users')')
    at /Users/photon/Development/3C/client/test/spec/services/dataservice.js:28
TypeError: 'undefined' is not an object (evaluating '$httpBackend.verifyNoOutstandingExpectations')
    at /Users/photon/Development/3C/client/test/spec/services/dataservice.js:21

I've tried various ways to inject $httpBackend including

1.

'use strict';

describe('Service: dataService', function () {
  // instantiate service
  var dataService, $httpBackend, $q;

  // load the service's module
  beforeEach(function() {
    module('clientApp');
    module('ngMock');

    inject(function (_stormpath_, _dataService_, _$httpBackend_, _$q_) {
      $httpBackend = _$httpBackend_;
      $q = _$q_;
      dataService = _dataService_;
    });
  });

  afterEach(function () {
    $httpBackend.verifyNoOutstandingExpectation97654321` ();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('GET /api/users when .getUsers()', function() {
    $httpBackend
      .expectGET('http:/localhost:3000/api/users')
      .respond(200);
    dataService.getUsers();
    $httpBackend.flush();
  });
});

  1. not including module('ngMock');
  2. using $injector to get dependencies

    beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend'); $q = $injector.get('$q'); dataService = $injector.get('dataService'); });

dataService is the service I am trying to test. It contains methods which use http GET requests to retrieve data from the backend.

Although stormpath-sdk-angular.js is mentioned in the stacktrace, I don't think that's the issue because this test should be isolated. As an aside, I am curious why it's there. The offending code is this:

this.$get = [
  'STORMPATH_CONFIG',
  function formEncoderServiceFactory(STORMPATH_CONFIG){

  function FormEncoderService(){
    var encoder = new UrlEncodedFormParser();
    this.encodeUrlForm = encoder.encode.bind(encoder);
    return this;
  }

  ...

Aucun commentaire:

Enregistrer un commentaire