angularjs - Unknown provider's error throw when injecting constant -
i having issues running karma tests.
i have service works well, in inject constant csrf_token
:
'use strict'; angular.module('app').factory("authenticationservice", function($http, $sanitize, sessionservice, flashservice, csrf_token) { var sanitizecredentials = function(credentials) { return { email: $sanitize(credentials.email), password: $sanitize(credentials.password), csrf_token: csrf_token }; }; ...
but when running grunt test
command, karma's error :
error: [$injector:unpr] unknown provider: csrf_tokenprovider <- csrf_token <- authenticationservice
update karma.conf :
// karma configuration // http://karma-runner.github.io/0.10/config/configuration-file.html module.exports = function(config) { config.set({ // base path, used resolve files , exclude basepath: '', // testing framework use (jasmine/mocha/qunit/...) frameworks: ['jasmine'], // list of files / patterns load in browser files: [ 'app/bower_components/angular/angular.js', 'app/bower_components/jquery/dist/jquery.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', 'app/bower_components/xdomain/dist/0.6/xdomain.js', 'app/bower_components/sass-bootstrap/dist/js/bootstrap.js', 'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 'app/scripts/*.js', 'app/scripts/**/*.js', 'test/mock/**/*.js', 'test/spec/**/*.js' ], // list of files / patterns 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 , executing tests whenever file changes autowatch: false, browsers: ['chrome'], // continuous integration mode // if true, capture browsers, run tests , exit singlerun: false }); };
and constant defined in bootstrap.js file instantiate before app starts :
'use strict'; angular.element(document).ready(function(){ var app = angular.module('app'); var $injector = angular.injector(['ng']); $injector.invoke(function($http, $rootscope) { $rootscope.$apply(function() { $http.get("http://something/token").then(function(response) { app.constant("csrf_token", response.data.csrf_token); angular.bootstrap(document, ['app']); }); }); }); });
do know solution karma stop thinking constant provider?
it seams document's ready event never thrown in jasmine test , therefore constant not defined in app
module. try mock csrf_token
constant in module configuration section of test:
javascript
describe('test', function() { beforeeach(function() { module('app', function($provide) { $provide.constant('csrf_token', 'mock_constant'); // <= mock constant }); }); // tests go here });
Comments
Post a Comment