尝试测试服务时,“TypeError:undefined不是对象”

时间:2016-06-13 21:09:24

标签: javascript angularjs unit-testing jasmine

我在这里有一个简单的服务,我正在测试并继续收到上述错误。做了很多研究,知道我的代码中有些东西是空的,但我找不到它。思考?在此先感谢:)

Config.js

'use strict';    

angular.module('App').service('configService', function(
  $rootScope, $http) {
  var configObj = null;
  return {

    getConfig: function() {
      if (configObj != null) {
        console.log("returning cached config");
        return configObj;
      }
      else {
        $http.get('conf.json').then(function(res) {
          $http.get(res.confLocation).then(function(
            locationResponse) {
            configObj = locationResponse;
            $rootScope.configObj = configObj;
            console.log($rootScope.configObj);
            return configObj;
          });
        });
      }
    }
  };
});

ConfigTest.js

'use strict';

describe('Service: configService', function() {

  // load the controller's module
  beforeEach(module('App'));

  var configService, $httpBackend, results;
  var tstConfig = {
    "confLocation": "local-dev-conf.json"
  };
  var tstConfigObj = {
    "AWS": {
      "region": "us-east-1",
      "endpoint": "http://localhost:8133"
    }
  };

  // Initialize the controller and a mock scope
  beforeEach(inject(function(_configService_, _$httpBackend_) {
    inject(function($rootScope) {
      $rootScope.USERNAME = 'TESTER';
      $rootScope.configObj = tstConfigObj;
    });

    configService = configService;
    $httpBackend = _$httpBackend_;
    // backend definition common for all tests
    $httpBackend.expectGET('conf.json').respond(tstConfig);
    $httpBackend.expectGET('local-dev-conf.json').respond(tstConfigObj);
  }));

  it('it should do something', inject(function() {

    results = configService.getConfig().then(function() { //ERROR HERE

      // What should I be expecting to check if it's parsing the file?
      // expect(configFile).toEqual("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}")

      console.log(results);
    });
    $httpBackend.flush();
  }));

2 个答案:

答案 0 :(得分:0)

问题在于您的spec / testcase文件。您需要将 configService 分配给configService。相反,您需要将configService分配给configService

此外,由于您从getConfig块调用了it方法,我建议您spyOn。但你没有间谍。它还应该返回一个名为then的函数。由于您没有这个,这就是您收到未定义错误的原因。

以下内容供您参考:

'use strict';

describe('Service: configService', function() {

  // load the controller's module
  beforeEach(module('App'));

  var configService, $httpBackend, results;
  var tstConfig = {
    "confLocation": "local-dev-conf.json"
  };
  var tstConfigObj = {
    "AWS": {
      "region": "us-east-1",
      "endpoint": "http://localhost:8133"
    }
  };

  // Initialize the controller and a mock scope
  beforeEach(inject(function(_configService_, _$httpBackend_) {
    inject(function($rootScope) {
      $rootScope.USERNAME = 'TESTER';
      $rootScope.configObj = tstConfigObj;
    });

    configService = _configService_;
    $httpBackend = _$httpBackend_;
    // backend definition common for all tests
    $httpBackend.expectGET('conf.json').respond(tstConfig);
    $httpBackend.expectGET('local-dev-conf.json').respond(tstConfigObj);

    spyOn(configService, 'getConfig').and.callFake(function(){
        return{
            then: function(){
                return "something";
            }
        }
    });

  }));

    it('it should do something', inject(function() {

        results = configService.getConfig().then(function() { //ERROR HERE

          // What should I be expecting to check if it's parsing the file?
          // expect(configFile).toEqual("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}")

          console.log(results);
        });
        //$httpBackend.flush();
    }));

});

希望这有帮助。

答案 1 :(得分:0)

这是因为您没有在else config.js声明中回复承诺{/ 1}}

'use strict';    

angular.module('App').service('configService', function(
  $rootScope, $http) {
  var configObj = null;
  return {

    getConfig: function() {
      if (configObj != null) {
        console.log("returning cached config");
        return configObj;
      }
      else {
        // return the promise here
        return $http.get('conf.json').then(function(res) {
          $http.get(res.confLocation).then(function(
            locationResponse) {
            configObj = locationResponse;
            $rootScope.configObj = configObj;
            console.log($rootScope.configObj);
            return configObj;
          });
        });
      }
    }
  };
});

您忘记返回承诺,因此函数返回undefined,这是"不是对象"并且没有.then