在测试用例中获取函数调用的错误

时间:2017-12-08 07:56:17

标签: angular typescript karma-jasmine

我有一个具有应用程序中使用的所有常量的类

export class Constants {
    static getDefault(): any {
        return {
                url_path : '/API Path',
                etc
            } 
        }
    }

我在服务中消耗我导入了这个类并使用import ConstantVal = app.base.Constants;并在url =' https://'中使用+ ConstantVal.getDefault()。urlpath;

在我的服务规范中,我收到错误

  

TypeError:无法读取属性' getDefault'未定义的函数

如何为我的测试用例添加此依赖项?
规格代码:

Spec Code:

/// <reference path='../../../../../typings/_references.ts' />

module app.base.apis {
   import ConstantVal = ptsp.base.Constants;
     describe('Service API calls tests ', function ():any {
 var apiService:any;
  var $httpBackend:any;
  //var constants:ptsp.base.Constants;
  beforeEach(angular.mock.module('App'));
  beforeEach(angular.mock.module('App.core'));
  beforeEach(angular.mock.inject(
     function ($injector:ng.auto.IInjectorService):any {
        apiService = $injector.get('apiService');
        $httpBackend = $injector.get('$httpBackend');
    }));

  it('It should call the DELETE API', function ():any {
     const constants = new Constants;
     ConstantVal.getDefault();  
});

}

1 个答案:

答案 0 :(得分:1)

getDefault定义为静态方法。因此,您需要使用类的全名来调用此方法。

Constants.getDefault(); 

另外,您应该将方法标记为公共方法:

   export class Constants {
        public static getDefault(): any {
            return {
                    url_path : '/API Path',
                    etc
                } 
            }
        }

规范代码:

/// <reference path='../../../../../typings/_references.ts' />

module app.base.apis {

'use strict';

import ConstantVal = ptsp.base.Constants;

declare var readJSON:any;

describe('Service API calls tests ', function ():any {

  var apiService:any;
  var $httpBackend:any;
  //var constants:ptsp.base.Constants;
  beforeEach(angular.mock.module('App'));
  beforeEach(angular.mock.module('App.core'));
  beforeEach(angular.mock.inject(
     function ($injector:ng.auto.IInjectorService):any {
        apiService = $injector.get('apiService');
        $httpBackend = $injector.get('$httpBackend');
    }));

  it('It should call the DELETE API', function ():any {
     const constants = new Constants;
     ConstantVal.getDefault();  
});
}