Angular 单元测试失败,无法注入服务
Angular Unit tests failing, not able to inject servies
这是我的karma.conf.js
`use strict`;
var path = require('path');
var conf = require('./gulp/conf');
var _ = require('lodash');
var wiredep = require('wiredep');
var pathSrcHtml = [
path.join(conf.paths.src, '/**/*.html')
];
function listFiles() {
var wiredepOptions = wiredep({
directory: 'src/bower_components',
exclude: [],
dependencies: true,
devDependencies: true
});
return wiredep(wiredepOptions).js
.concat([
path.join(conf.paths.src, '/app/**/*.module.js'),
path.join(conf.paths.src, '/app/**/*.js'),
path.join(conf.paths.src, '/app/*.spec.js')
])
.concat(pathSrcHtml);
}
module.exports = function(config) {
var files=['https://maps.googleapis.com/maps/api/js?sensor=false'].concat(listFiles());
var configuration = {
files: files,
singleRun: true,
autoWatch: false,
ngHtml2JsPreprocessor: {
stripPrefix: conf.paths.src ,
moduleName: 'truelocal'
},
logLevel: 'WARN',
frameworks: ['jasmine', 'angular-filesort'],
angularFilesort: {
whitelist: [path.join(conf.paths.src, '/**/!(*.html|*.spec|*.mock).js')]
},
browsers : ['PhantomJS'],
plugins : [
'karma-phantomjs-launcher',
'karma-angular-filesort',
'karma-coverage',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
coverageReporter: {
type : 'html',
dir : 'coverage/'
},
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
captureConsole: true,
reporters: ['progress']
};
// This is the default preprocessors configuration for a usage with Karma cli
// The coverage preprocessor in added in gulp/unit-test.js only for single tests
// It was not possible to do it there because karma doesn't let us now if we are
// running a single test or not
configuration.preprocessors = {};
pathSrcHtml.forEach(function(path) {
configuration.preprocessors[path] = ['ng-html2js'];
});
// This block is needed to execute Chrome on Travis
// If you ever plan to use Chrome and Travis, you can keep it
// If not, you can safely remove it
// https://github.com/karma-runner/karma/issues/1144#issuecomment-53633076
if(configuration.browsers[0] === 'Chrome' && process.env.TRAVIS) {
configuration.customLaunchers = {
'chrome-travis-ci': {
base: 'Chrome',
flags: ['--no-sandbox']
}
};
configuration.browsers = ['chrome-travis-ci'];
}
config.set(configuration);
};
//Bower.json
{
"name": "truelocal",
"version": "0.0.0",
"dependencies": {
"angular": "~1.4.10",
"angular-animate": "~1.4.10",
"angular-aria": "~1.4.10",
"angular-bootstrap": "~0.13.4",
"angular-cookies": "~1.4.10",
"angular-local-storage": "~0.2.3",
"angular-messages": "~1.4.10",
"angular-resource": "~1.4.10",
"angular-sanitize": "~1.4.10",
"angular-toastr": "~1.5.0",
"angular-touch": "~1.4.10",
"animate.css": "~3.4.0",
"bootstrap": "~3.3.5",
"jquery": "~1.11.3",
"malarkey": "yuanqing/malarkey#~1.3.1",
"moment": "~2.10.6",
"moment-parseformat": "~1.1.3",
"ng-file-upload-shim": "~9.1.2",
"jQuery.dotdotdot": "~1.7.4",
"lodash": "3.10.1",
"angular-ui-router": "~0.2.18",
"markerclustererplus": "~2.1.4",
"ng-fastclick": "~1.0.2",
"angular-swipe": "~0.1.0",
"angular-bindonce": "~0.3.3",
"angular-loading-bar": "~0.9.0",
"jquery-placeholder": "~2.3.1",
"angular-shims-placeholder": "^0.4.7"
},
"devDependencies": {
"angular-mocks": "~1.4.10"
},
"overrides": {
"bootstrap": {
"main": [
"dist/css/bootstrap.css",
"dist/fonts/glyphicons-halflings-regular.eot",
"dist/fonts/glyphicons-halflings-regular.svg",
"dist/fonts/glyphicons-halflings-regular.ttf",
"dist/fonts/glyphicons-halflings-regular.woff",
"dist/fonts/glyphicons-halflings-regular.woff2"
]
}
},
"resolutions": {
"jquery": "~1.11.3",
"angular": "^1.5.7"
}
}
这是一个现有项目,编写了 200 多个单元测试,代码可能需要一些更新,但我无法使用依赖服务执行任何单元测试。
我猜 angular-mocks 导致了一个问题,如果我在依赖项中向上移动它,我不会得到模块丢失错误。但我仍然没有在单元测试中得到其他依赖项。
Karma.conf.js 项目中缺少一个文件。一旦所有文件都被提供给测试 conf。测试就像一个魅力!
这是我的karma.conf.js
`use strict`;
var path = require('path');
var conf = require('./gulp/conf');
var _ = require('lodash');
var wiredep = require('wiredep');
var pathSrcHtml = [
path.join(conf.paths.src, '/**/*.html')
];
function listFiles() {
var wiredepOptions = wiredep({
directory: 'src/bower_components',
exclude: [],
dependencies: true,
devDependencies: true
});
return wiredep(wiredepOptions).js
.concat([
path.join(conf.paths.src, '/app/**/*.module.js'),
path.join(conf.paths.src, '/app/**/*.js'),
path.join(conf.paths.src, '/app/*.spec.js')
])
.concat(pathSrcHtml);
}
module.exports = function(config) {
var files=['https://maps.googleapis.com/maps/api/js?sensor=false'].concat(listFiles());
var configuration = {
files: files,
singleRun: true,
autoWatch: false,
ngHtml2JsPreprocessor: {
stripPrefix: conf.paths.src ,
moduleName: 'truelocal'
},
logLevel: 'WARN',
frameworks: ['jasmine', 'angular-filesort'],
angularFilesort: {
whitelist: [path.join(conf.paths.src, '/**/!(*.html|*.spec|*.mock).js')]
},
browsers : ['PhantomJS'],
plugins : [
'karma-phantomjs-launcher',
'karma-angular-filesort',
'karma-coverage',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
coverageReporter: {
type : 'html',
dir : 'coverage/'
},
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
captureConsole: true,
reporters: ['progress']
};
// This is the default preprocessors configuration for a usage with Karma cli
// The coverage preprocessor in added in gulp/unit-test.js only for single tests
// It was not possible to do it there because karma doesn't let us now if we are
// running a single test or not
configuration.preprocessors = {};
pathSrcHtml.forEach(function(path) {
configuration.preprocessors[path] = ['ng-html2js'];
});
// This block is needed to execute Chrome on Travis
// If you ever plan to use Chrome and Travis, you can keep it
// If not, you can safely remove it
// https://github.com/karma-runner/karma/issues/1144#issuecomment-53633076
if(configuration.browsers[0] === 'Chrome' && process.env.TRAVIS) {
configuration.customLaunchers = {
'chrome-travis-ci': {
base: 'Chrome',
flags: ['--no-sandbox']
}
};
configuration.browsers = ['chrome-travis-ci'];
}
config.set(configuration);
};
//Bower.json
{
"name": "truelocal",
"version": "0.0.0",
"dependencies": {
"angular": "~1.4.10",
"angular-animate": "~1.4.10",
"angular-aria": "~1.4.10",
"angular-bootstrap": "~0.13.4",
"angular-cookies": "~1.4.10",
"angular-local-storage": "~0.2.3",
"angular-messages": "~1.4.10",
"angular-resource": "~1.4.10",
"angular-sanitize": "~1.4.10",
"angular-toastr": "~1.5.0",
"angular-touch": "~1.4.10",
"animate.css": "~3.4.0",
"bootstrap": "~3.3.5",
"jquery": "~1.11.3",
"malarkey": "yuanqing/malarkey#~1.3.1",
"moment": "~2.10.6",
"moment-parseformat": "~1.1.3",
"ng-file-upload-shim": "~9.1.2",
"jQuery.dotdotdot": "~1.7.4",
"lodash": "3.10.1",
"angular-ui-router": "~0.2.18",
"markerclustererplus": "~2.1.4",
"ng-fastclick": "~1.0.2",
"angular-swipe": "~0.1.0",
"angular-bindonce": "~0.3.3",
"angular-loading-bar": "~0.9.0",
"jquery-placeholder": "~2.3.1",
"angular-shims-placeholder": "^0.4.7"
},
"devDependencies": {
"angular-mocks": "~1.4.10"
},
"overrides": {
"bootstrap": {
"main": [
"dist/css/bootstrap.css",
"dist/fonts/glyphicons-halflings-regular.eot",
"dist/fonts/glyphicons-halflings-regular.svg",
"dist/fonts/glyphicons-halflings-regular.ttf",
"dist/fonts/glyphicons-halflings-regular.woff",
"dist/fonts/glyphicons-halflings-regular.woff2"
]
}
},
"resolutions": {
"jquery": "~1.11.3",
"angular": "^1.5.7"
}
}
这是一个现有项目,编写了 200 多个单元测试,代码可能需要一些更新,但我无法使用依赖服务执行任何单元测试。 我猜 angular-mocks 导致了一个问题,如果我在依赖项中向上移动它,我不会得到模块丢失错误。但我仍然没有在单元测试中得到其他依赖项。
Karma.conf.js 项目中缺少一个文件。一旦所有文件都被提供给测试 conf。测试就像一个魅力!