jeudi 5 novembre 2015

Unknown Provider: $scopeProvider <-$scope <- QueryBuilderController

I am currently trying to test my angular application and i'm running into a problem with my test failing but the code is working...I can't get the controller to actually be created. I'm using Karma with Phantomjs through gulp. The error i'm getting is this:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <-QueryBuilderController

Here is my test:

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;
 });
});

The interesting thing about this is I have a nearly identical test that does work and load that looks like this:

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;
});
});

Here is the 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;
  }
}) ();

And here is the module that the controller belongs to:

 (function() {
 'use strict';

 angular
    .module('app.query', [
        'app.core',
        'elasticsearch',
        'angular-elastic-builder',
        'angularUtils.directives.dirPagination'
    ]);
})();

Here is the gulp task:

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();
    }
 }
}

And the 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

});
};

And last but not least, the project structure:

    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

Sorry for all of the information, tried to include all of the relative information. All help is appreciated, thank you in advance!

Aucun commentaire:

Enregistrer un commentaire