我目前正在尝试测试我的角度应用程序,并且我遇到问题,我的测试失败但代码正在运行......我无法让控制器实际创建。我通过gulp使用Karma和Phantomjs。我得到的错误是:
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <-QueryBuilderController
这是我的测试:
describe('QueryBuilderController', function(){
var controller;
beforeEach(function(){
bard.appModule('app.query');
bard.inject(this,'$controller');
controller = $controller('QueryBuilderController');
});
it('should exist', function(){
expect(controller).to.exist;
});
});
有趣的是,我有一个几乎相同的测试,可以正常工作和加载:
describe('ShellController', function(){
var controller;
beforeEach(function(){
bard.appModule('app.shell');
bard.inject('$controller');
controller = $controller('ShellController');
});
it('should exist', function(){
expect(controller).to.exist;
});
});
这是QueryBuilderController:
(function () {
'use strict';
angular.module('app.query')
.controller('QueryBuilderController', QueryBuilderController);
QueryBuilderController.$inject = ['QueryFactory', '$scope'];
function QueryBuilderController (QueryFactory, $scope){
var vm = this;
vm.displayQuery = true;
}
}) ();
这是控制器所属的模块:
(function() {
'use strict';
angular
.module('app.query', [
'app.core',
'elasticsearch',
'angular-elastic-builder',
'angularUtils.directives.dirPagination'
]);
})();
这是gulp任务:
gulp.task('test', ['vet'], function(done){
startTests(true, done);
});
///////
function startTests(singleRun, done) {
var child;
var excludeFiles = [];
var fork = require('child_process').fork;
var karma = require('karma').server;
var serverSpecs = config.serverIntegrationSpecs;
if (args.startServers) {
console.log('Starting servers');
var savedEnv = process.env;
savedEnv.NODE_ENV = 'dev';
savedEnv.PORT = 8888;
child = fork(config.nodeServer);
} else {
if (serverSpecs && serverSpecs.length) {
excludeFiles = serverSpecs;
}
}
karma.start({
configFile: __dirname + '/karma.conf.js',
exclude: excludeFiles,
singleRun: !!singleRun
}, karmaCompleted);
////////////////
function karmaCompleted(karmaResult) {
console.log('Karma completed');
if (child) {
console.log('shutting down the child process');
child.kill();
}
if (karmaResult === 1) {
done('karma: tests failed with code ' + karmaResult);
} else {
done();
}
}
}
和GulpConfig:
module.exports = function(){
var client = './src/client/';
var clientApp = client + 'app/';
var temp = './.tmp/';
var server = './src/server/';
var report = './report/';
var wiredep = require('wiredep');
var bowerFiles = wiredep({devDependencies: true})['js'];
var root = './';
var config = {
// File Paths
temp: './.tmp/',
alljs: [
'./src/**/*.js',
'./*.js'
],
htmlTemplates: clientApp + '**/*.html',
html: clientApp + '**/*.html',
client: client,
index: client + 'index.html',
build: './static/',
fonts: './bower_components/font-awesome/fonts/**/*.*',
images: client + 'images/**/*.*',
js: [
clientApp + '**/*.module.js',
clientApp + '**/*.js',
'!' + clientApp + '**/*.spec.js'
],
templateCache: {
file: 'templates.js',
options: {
module: 'app.core',
standAlone: false,
root: 'app/'
}
},
sass: client + 'styles/styles.sass',
css: [temp + 'styles.css', client + 'assetts/*.css'],
// Bower and NPM locations
bower: {
json: require('./bower.json'),
directory: './bower_components/',
ignorePath: '../..'
},
specHelpers: client +'specHelpers/*.js',
// Node Settings
nodeServer: './src/server/dev_server.js',
port: 3000,
server: server,
// Optimized File Names
optimized: {
app: 'app.js',
lib: 'lib.js'
},
// Packages for bower and NPM
packages: [
'./package.json',
'./bower.json'
],
root: root
};
config.getWiredepDefaultOptions = function(){
var options = {
bowerJson: config.bower.json,
directory: config.bower.directory,
ignorePath: config.bower.ignorePath
};
return options;
};
config.karma = getKarmaOptions();
return config;
///////////////////////////////
function getKarmaOptions() {
var options = {
files: [].concat(
bowerFiles,
config.specHelpers,
clientApp + '**/*.module.js',
clientApp + '**/*.js'
// config.serverIntegrationSpecs
),
exclude: [],
coverage: {
dir: report + 'coverage',
reporters: [
// reporters not supporting the `file` property
{type: 'html', subdir: 'report-html'},
{type: 'lcov', subdir: 'report-lcov'},
{type: 'text-summary'} //, subdir: '.', file: 'text-
summary.txt'}
]
},
preprocessors: {}
};
options.preprocessors[clientApp + '**/!(*.spec)+(.js)'] = ['coverage'];
return options;
}
};
Karma.Conf:
module.exports = function(config){
var gulpConfig = require('./gulp.config')();
config.set({
basePath: './',
files: gulpConfig.karma.files,
exclude: gulpConfig.karma.exclude,
proxies: {
'/': 'http://localhost:8888/'
},
// preprocessors: gulpConfig.karma.preprocessors,
reporters: ['progress', 'coverage'],
coverageReporter: {
dir: gulpConfig.karma.coverage.dir,
reporters: gulpConfig.karma.coverage.reporters
},
// web server port
port: 9876,
plugins: [
'karma-mocha',
'karma-chai',
'karma-sinon',
'karma-sinon-chai',
'karma-phantomjs-launcher',
'karma-coverage'
],
// enable / disable colors in the output (reporters and logs)
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
frameworks: ['mocha', 'chai', 'sinon', 'sinon-chai'],
browsers: ['PhantomJS'],
singleRun: false
});
};
最后但并非最不重要的是,项目结构:
Project
|-- gulpfile.js
|-- gulp.config.js
|-- karma.conf.js
|-- package.json
|-- bower.json
`-- src
`-- client
|-- assetts
|-- images
|-- specHelpers
|-- styles
|-- index.html
|-- specs.html
`-- app
|-- angular-elastic-builder.js
|-- app.module.js
|-- blocks
| |-- exception
| | |-- exception-handler.provider
| | |-- exception.js
| | `-- exception.module.js
| |-- logger
| | |-- logger.js
| | `-- logger.module.js
| `-- router
| |-- router.module.js
| `-- router-helper.provider.js
|-- core
|-- dashboard
|-- query
| |-- querybuilder.controller.js
| |-- querybuilder.html
| |-- query.module.js
| `-- querycontroller.spec.js
`-- shell
|-- shell.module.js
|-- shell.controller.spec.js
|-- shell.controller.js
`-- shell.html
对不起所有信息,试图包含所有相关信息。感谢所有帮助,提前谢谢!
更新:当我不使用bard时,它似乎已经过去了,bard.js是否可能导致我的某个模块依赖项出现某种问题?
/* jshint -W117, -W030 */
describe('QueryBuilderController', function(){
// beforeEach(function(){
// module('app.query');
// bard.inject('$controller');
// controller = $controller('QueryBuilderController');
// });
beforeEach(module('app.query'));
var $controller;
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
it('should exist', function(){
var $scope = {};
var controller = $controller('QueryBuilderController', { $scope: $scope });
expect(controller).to.exist;
});
});
答案 0 :(得分:0)
您是否正确注入了依赖项?根据{{3}}你需要做这样的事情:
angular.module('myApp', [])
.controller('MyController', ['myService', function (myService) {
// Do something with myService
}]);
如果我没有正确地声明我的依赖项,我的代码被缩小时,我才遇到这个问题,但我想知道在你的情况下它是否是同样的事情。