AngularJS单元测试-未定义类

AngularJS单元测试-未定义类,angularjs,typescript,unit-testing,karma-jasmine,Angularjs,Typescript,Unit Testing,Karma Jasmine,我正试图为一些控制器编写一些基本测试。我传入一些使用其他服务的服务。传入的一个服务,“目录服务”用户可以使用一个名为“度量服务”的服务。此度量调用在文件顶部声明的帮助程序,它从配置文件的“我的依赖项”部分中定义的包中提取。代码可以正常工作/构建,但测试表明此助手未定义。我错过了什么 这就是测试 beforeEach(module("photonControllersPreSession")); var $controller; var $rootScope; var $scope

我正试图为一些控制器编写一些基本测试。我传入一些使用其他服务的服务。传入的一个服务,“目录服务”用户可以使用一个名为“度量服务”的服务。此度量调用在文件顶部声明的帮助程序,它从配置文件的“我的依赖项”部分中定义的包中提取。代码可以正常工作/构建,但测试表明此助手未定义。我错过了什么

这就是测试

 beforeEach(module("photonControllersPreSession"));

  var $controller;
  var $rootScope;
  var $scope;
  var createController;

  var $window;
  var $location;
  var loggerService;
  var controller;

  beforeEach(
    angular.mock.module( function(
      _$controller_,
      _$rootScope_,
      _$window_,
      _$location_,
      _loggerService_
    ) {

      $controller = _$controller_;
      $rootScope = _$rootScope_;
      $window = _$window_;
      $location = _$location_;
      loggerService = _loggerService_;
      $scope = $rootScope.$new();

      controller = $controller("CatalogController", {
        $scope: $scope,
        $location: $location,
        $window: $window,
        loggerService: loggerService
      });


    })
  );
这是调用未定义帮助程序的服务(由logger服务使用)

declare var HelperUtils;

export class MetricsService {

public static $inject: Array<string> = ["$rootScope", "$window", Constants.Photon.SERVICE_LOGGER];
private ubmHelper;
private dimensions: Array<any> = [];
private metricList = null;
private TAG = "MetricsService";
private trackingScopes: any = {};
private isEditing = false;

constructor(protected $rootScope, protected  $window:Interfaces.IPhotonWindowObject, protected loggerService: LoggerService, protected hiddenBrowserDetectionService: Services.HiddenBrowserDetectionService) {
  let ubmOptions = {
    idleInterval: 3000,
    submitInterval: 5000,
    header: {},
    logLevel: HelperUtils.LogLevelEnum.INFO,
    properties: {
      test : accountID
    }
  };
声明var HelperUtils;
出口级计量服务{
公共静态$inject:Array=[“$rootScope”,“$window”,Constants.Photon.SERVICE_LOGGER];
私人ubmHelper;
私有维度:数组=[];
私有度量列表=null;
private TAG=“MetricsService”;
私有跟踪范围:any={};
私人isEditing=false;
构造函数(受保护的$rootScope,受保护的$window:Interfaces.IPhotonWindowObject,受保护的loggerService:loggerService,受保护的hiddenBrowserDetectionService:Services.hiddenBrowserDetectionService){
让ubmOptions={
idleInterval:3000,
次电信:5000,
标题:{},
日志级别:HelperUtils.LogLevelEnum.INFO,
特性:{
测试:accountID
}
};
我的业力配置如下

module.exports = function(config) {
  "use strict";

  config.set({
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // base path, that will be used to resolve files and exclude
    basePath: "../",

    // testing framework to use (jasmine/mocha/qunit/...)
    // as well as any additional frameworks (requirejs/chai/sinon/...)
    frameworks: [
      "jasmine",
      "karma-typescript"
    ],

    // list of files / patterns to load in the browser
    files: [
      "vendor/jquery/dist/jquery.js",
      "vendor/angular/angular.js",
      "vendor/angular-animate/angular-animate.js",
      "vendor/angular-cookies/angular-cookies.js",
      "vendor/angular-messages/angular-messages.js",
      "vendor/angular-resource/angular-resource.js",
      "vendor/angular-route/angular-route.js",
      "vendor/angular-sanitize/angular-sanitize.js",
      "vendor/angular-touch/angular-touch.js",
      "vendor/angular-mocks/angular-mocks.js",
      //"app/photon-app.js",
      "app/scripts/commons/providers/ui-notification.js",
      "app/scripts/commons/constants.ts",
      "app/constants-global.ts",
      "app/scripts/commons/interfaces/*.ts",
      "app/scripts/commons/factories/*.ts",
      "app/scripts/commons/services/*.ts",
      "app/**/*.ts",
      "test/specs/**/*.ts",
      {
        pattern: '**/*.js.map',
        included: false
      }
    ],

    // list of files / patterns to exclude
    exclude: [
      "photon/**/*.d.ts",
      "app/scripts/commons/services/message-service.ts" //TODO: please add this servcie back after basic sannity test are passing
    ],

    //This is neede to load all *spec.ts 
    mime: {
      'text/x-typescript': ['ts']
    },


    karmaTypescriptConfig: {
      "compilerOptions": {
        "moduleResolution": "node",
        "noLib": false,
        "removeComments": true,
        "target": "es5",
        // or es3, es6
        "declaration": false
        // Not compile .d.ts files

      },
      "exclude": [
        "photon/**/*.d.ts",
        "app/scripts/commons/services/message-service.ts" //TODO: please add this servcie back after basic sannity test are passing
      ],
      "include": [
        "app/scripts/commons/constants.ts",
        "app/constants-global.ts",
        "app/scripts/commons/interfaces/*.ts",
        "app/scripts/commons/factories/*.ts",
        "app/scripts/commons/services/*.ts",
        "app/**/*.ts"
        //"test/specs/**/*.ts"
      ]
    },

    // web server port
    port: 8080,

    browsers: [
      "Chrome"
    ],

    // Which plugins to enable
    plugins: [
      "karma-phantomjs-launcher",
      "karma-chrome-launcher",
      "karma-jasmine",
      "karma-coverage",
      "karma-junit-reporter",
      "karma-typescript"
    ],

    reporters: ["progress", "junit", "coverage"],

    preprocessors: {
      "app/**/*.ts": ['karma-typescript']
    },
    bundlerOptions: {
      transforms: [
        require("karma-typescript-es6-transform")()
      ]
    },

    coverageReporter: {
      reporters: [{
        type: 'cobertura',
        dir: 'coverage',
        file: 'coverage.xml'
      }, {
        type: 'html',
        dir: 'coverage'
      }, {
        type: 'text-summary'
      }]
    },

    junitReporter: {
      //outputDir: "../build/brazil-unit-tests", // results will be saved as $outputDir/$browserName.xml
      //outputDir: "../build/brazil-unit-tests/test-results.xml", // results will be saved as $outputDir/$browserName.xml
      outputFile: "../build/brazil-unit-tests/test-results.xml", // if included, results will be saved as $outputDir/$browserName/$outputFile
      suite: "PhotonPortal", // suite will become the package name attribute in xml testsuite element
      useBrowserName: false, // add browser name to report and classes names
      nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
      classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
      properties: {} // key value pair of properties to add to the <properties> section of the report
    },

    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false,

    colors: true,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_DEBUG,


  });
module.exports=函数(配置){
“严格使用”;
config.set({
//启用/禁用监视文件并在任何文件更改时执行测试
自动观察:没错,
//基本路径,将用于解析文件和排除
基本路径:“../”,
//要使用的测试框架(jasmine/mocha/qunit/…)
//以及任何其他框架(requirejs/chai/sinon/…)
框架:[
“茉莉花”,
“因果报应打字稿”
],
//要在浏览器中加载的文件/图案列表
档案:[
“vendor/jquery/dist/jquery.js”,
“vendor/angular/angular.js”,
“供应商/angular animate/angular animate.js”,
“供应商/angular cookies/angular cookies.js”,
“vendor/angular messages/angular messages.js”,
“供应商/角度资源/角度资源.js”,
“供应商/angular route/angular route.js”,
“供应商/angular sanitize/angular sanitize.js”,
“供应商/angular touch/angular touch.js”,
“供应商/angular mocks/angular mocks.js”,
//“app/photon app.js”,
“app/scripts/commons/providers/ui notification.js”,
“app/scripts/commons/constants.ts”,
“app/constants global.ts”,
“app/scripts/commons/interfaces/*.ts”,
“app/scripts/commons/factories/*.ts”,
“app/scripts/commons/services/*.ts”,
“app/***.ts”,
“测试/规格/***/.ts”,
{
模式:'***.js.map',
包括:假
}
],
//要排除的文件/模式列表
排除:[
“光子/***/.d.ts”,
“app/scripts/commons/services/message service.ts”//TODO:请在基本安全测试通过后重新添加此服务
],
//这需要加载所有*规格
默剧:{
'text/x-typescript':['ts']
},
karmaTypescriptConfig:{
“编译器选项”:{
“moduleResolution”:“节点”,
“noLib”:错,
“removeComments”:正确,
“目标”:“es5”,
//或es3、es6
“声明”:虚假
//不编译.d.ts文件
},
“排除”:[
“光子/***/.d.ts”,
“app/scripts/commons/services/message service.ts”//TODO:请在基本安全测试通过后重新添加此服务
],
“包括”:[
“app/scripts/commons/constants.ts”,
“app/constants global.ts”,
“app/scripts/commons/interfaces/*.ts”,
“app/scripts/commons/factories/*.ts”,
“app/scripts/commons/services/*.ts”,
“app/***.ts”
//“测试/规格/***/.ts”
]
},
//web服务器端口
端口:8080,
浏览器:[
“铬”
],
//要启用哪些插件
插件:[
“卡玛幻影发射器”,
“卡玛铬发射器”,
“因果报应的茉莉花”,
“业力覆盖”,
“karma junit记者”,
“因果报应打字稿”
],
记者:[“进步”、“少年”、“报道”],
预处理器:{
“app/***.ts”:['karma-typescript']
},
捆绑选项:{
转换:[
需要(“karma-typescript-es6-transform”)()
]
},
报道员:{
记者:[{
类型:“cobertura”,
目录:“覆盖范围”,
文件:“coverage.xml”
}, {
键入:“html”,
目录:“覆盖范围”
}, {
键入:“文本摘要”
}]
},
junitReporter:{
//outputDir:“../build/brazil unit tests”,//结果将另存为$outputDir/$browserName.xml
//outputDir:“../build/brazic unit tests/test results.xml”,//结果将另存为$outputDir/$browserName.xml
outputFile:“../build/brazic unit tests/test results.xml”,//如果包含,结果将另存为$outputDir/$browserName/$outputFile
suite:“PhotonPortal”//suite将成为xml testsuite元素中的包名属性
useBrowserName:false,//将浏览器名称添加到报表和类名称中
nameFormatter:undefined,//用于自定义xml testcase元素中的name属性的函数(浏览器、结果)
classNameFormatter:undefined,//用于自定义xml testcase元素中classname属性的函数(浏览器、结果)
属性:{}//要添加到报表节的属性的键值对
},
//连续集成模式
//如果为true,则捕获浏览器,运行测试并退出
单程:错,
颜色:对,
//记录级别
//可能的值:LOG|U DISABLE | LOG|U ERROR | LOG|U WARN | LOG|U INFO | LOG|U DEBUG
日志级别:config.LOG\u调试,
});

这只是供您考虑的一些注释,而不是答案。谈到单元测试,重点是测试控制器/服务/过滤器或其他什么
 beforeEach(
    angular.mock.module( function(
      _$controller_,
      _$rootScope_,
      _$window_,
      _$location_,
    ) {
      $controller = _$controller_;
      $rootScope = _$rootScope_;
      $window = _$window_;
      $location = _$location_;
      loggerService = jasmine.createSpyObj('loggerService', ['method1', 'method2']);
      $scope = $rootScope.$new();

      controller = $controller("CatalogController", {
        $scope: $scope,
        $location: $location,
        $window: $window,
        loggerService: loggerService
      });
    })

    it('should test some CatalogController method', function () {
        CatalogController.someMethod();

        expect(loggerService.method1).toHaveBeenCalledTimes(1);
        expect(loggerService.method1).toHaveBeenCalledWith('whatever');
    });
  );