评估$ httpBackend.expectGet('url')会导致'undefined'不是函数错误

时间:2015-09-28 20:08:07

标签: angularjs unit-testing httpbackend stormpath

我正在尝试使用$httpBackend为服务编写单元测试来模拟http请求。目前我只是想让expectGET工作,但我收到以下错误:

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

我尝试了各种方法来注入$ httpBackend,包括

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.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('GET /api/users when .getUsers()', function() {
    $httpBackend
      .expectGET('http:/localhost:3000/api/users')
      .respond(200);
    dataService.getUsers();
    $httpBackend.flush();
  });
});
  1. 不包括module('ngMock');
  2. 使用$injector获取依赖关系
  3. IE:

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

    dataService是我要测试的服务。它包含使用http GET请求从后端检索数据的方法。

    虽然堆栈跟踪中提到stormpath-sdk-angular.js,但我不认为这是问题,因为应该隔离此测试。顺便说一句,我很好奇为什么会这样。抛出错误的stormpath-sdk-angular.js代码是:

    this.$get = [
      'STORMPATH_CONFIG',
      function formEncoderServiceFactory(STORMPATH_CONFIG){
    
      function FormEncoderService(){
        var encoder = new UrlEncodedFormParser();
        this.encodeUrlForm = encoder.encode.bind(encoder);
        return this;
      }
    
      ...
    

0 个答案:

没有答案