我已经拥有了一个使用npm,grunt,bower ......和karma + jasmin的角度项目。 我被要求使用karma和jasmin为项目设置一些测试。 业力已经在项目中设置但从未使用过。 当我运行'grunt test'时,我会在所有服务上出现注入错误,如下所示。
错误:[$ injector:unpr]未知提供者:excelparserserviceProvider< - excelparserservice http://errors.angularjs.org/1.2.6/ $注射器/ unprp0 = excelparserserviceProvider%20%3 C-%20excelparserservice
已经有一个看起来像这样的karma.conf.js了。 我没有更改karma.conf文件中的任何内容,只是将项目中使用的一些库添加到Files:[]下的列表中。
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
// i manually added the ones from here
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/geocoder-js/dist/geocoder.js',
'app/bower_components/js-xlsx/dist/xlsx.core.min.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'app/bower_components/d3/d3.js',
'app/bower_components/angular-file-upload/angular-file-upload.js',
// to here.
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
路径,
'app / scripts / * .js' - >导致app.js和config.js
'app / scripts / ** / * .js' - >指向所有服务控制器和指令
'test / mock / ** / * .js' - >是不存在的
'测试/规格/ ** / * JS' - >包含所有测试文件
有一些测试文件对应于applikation的每个部分。我被告知的是自动生成的。所以如果他们应该包含错误,我觉得很奇怪。但是与excpelparserservice注入错误有关的那个是。
'use strict';
describe('Service: excelparserservice', function () {
// load the service's module
beforeEach(module('batchUploadApp'));
// instantiate service
var Excelparserservice;
beforeEach(inject(function (_excelparserservice_) {
Excelparserservice = _excelparserservice_;
}));
it('should do something', function () {
expect(!!Excelparserservice).toBe(true);
});
});
服务声明如下所示。
'use strict';
angular.module('batchUploadApp')
.service('ExcelParserService',
function ExcelParserService($q, ExcelvalidationService, GeoLocationService) {
一般的应用程序。
希望我的解释是有用的:) 谢谢。
答案 0 :(得分:0)
您可以像这样定义和注册您的服务:
ExcelParserService
表示您以beforeEach(inject(function (_excelparserservice_) {
名称注册。另一方面,当您尝试将服务注入test时,您将使用小写的名称:
beforeEach(inject(function (_ExcelParserService_) {
两个名称必须匹配,因此解决方案是更改参数的名称:
overflow:hidden