Service not defining when injecting it for Karma testing - AngularJS -
i've seen few of these questions on internet, none of answers seem solve problem. have service works in served app, when test in karma not being defined.
i've got modules , associated controllers, directives , services in src
folder. structure of file follows:
angular.module('cst-error-framework', ['md.alerts', 'md.input', 'md.modals']); angular.module('md.alerts', []) .directive(//..associated directives here angular.module('md.input', []) //blah blah blah angular.module('md.modals', []) //directives , controllers .factory('mderrorhandler', function () { //contents here });
i know seems bit cumbersome there other reasons have in 1 file.
then spec mderrorhandler
follows:
describe('service errorhandler', function () { beforeeach(function() { module('cst-error-framework'); angular.mock.module('templates'); }); var mderrorhandler; beforeeach(inject(function(_mderrorhandler_){ mderrorhandler = _mderrorhandler_; console.log(mderrorhandler); }));
here karma.conf.js
:
module.exports = function(config) { config.set({ basepath: '', frameworks: ['jasmine'], files: [ 'bower_components/jquery/dist/jquery.js', 'bower_components/angular/angular.js', 'bower_components/angular-sanitize/angular-sanitize.js', 'bower_components/angular-mocks/angular-mocks.js', 'dist/cst-error-framework.js', 'dist/cst-error-framework.css', 'src/**/templates/*.html', 'src/helpers/*.js', 'src/**/test/*.spec.js' ], preprocessors : { 'src/**/templates/*.html': 'ng-html2js' }, nghtml2jspreprocessor: { stripprefix: 'src/', modulename: 'templates' }, exclude: [], port: 9876, loglevel: config.log_info, autowatch: true, browsers: [process.env.travis ? 'firefox' : 'chrome'], singlerun: false }); 'use strict'; };
then when run karma tests, mderrorhandler
not being defined, result of console.log
[]
.
any idea what's going on? order karma loading stuff? avoid having refactor main .js
file.
edit starting suspect maybe in gulpfile causing issues? here karma gulp task:
gulp.task('karma', ['build'], function(done) { karma.start({ configfile: __dirname + '/karma.conf.js', singlerun: true }, done); });
when instantiate module in test, don't need function declaration.
so instead of
beforeeach(function() { module('cst-error-framework'); angular.mock.module('templates'); });
try
beforeeach(module('cst-error-framework'); beforeeach(angular.mock.module('templates');
Comments
Post a Comment