我正在尝试为我拥有的某些控制器编写一些基本测试。我传递了一些使用其他服务的服务。传入的一项服务“目录服务”为用户提供了一种称为“度量标准服务”的服务。此指标调用在文件顶部声明的Helper,并将其从Config文件的“依赖项”部分中定义的包中拉出。该代码可以正常工作/构建,但是测试表明该Helper未定义。我在这里想念什么?
这是测试
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
});
})
);
这是调用未定义的帮助程序的服务(由记录程序服务使用)
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
}
};
我的业力配置低于
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,
});
答案 0 :(得分:1)
这只是一些注意事项,不能作为答案。说到单元测试,重点是要独立测试控制器/服务/过滤器或其他任何 ,这就是为什么它称为 unit 的原因。还有另一种类型的测试,称为 E2E ,即End to End
,涵盖了所有代码,但这是另一回事。
所以我建议您像这样重写代码
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');
});
);
因此,正如您所看到的,我将loggerService
放在模拟中,其中method1
,method2
是在CatalogController
中使用的方法。这就是我在谈论的内容-单元测试只是为了检查loggerService.method1
在某个特定时刻是否已被调用,您无需检查method1
或method2
到底做了什么,这是您应在loggerService
测试中测试的内容,但这与CatalogController
测试无关。但是,我不确定HelperUtils
不确定是什么问题。