Inconnu du fournisseur d'erreur jeter lors de l'injection constante
-
23-12-2019 - |
Question
Je vais avoir quelques problèmes de fonctionnement de mon karma tests.
J'ai de ce service qui fonctionne bien, qui m'injecter une constante 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
};
};
...
Mais lors de l'exécution grunt test
de commande, le Karma de l'erreur est :
Error: [$injector:unpr] Unknown provider: CSRF_TOKENProvider <- CSRF_TOKEN <- AuthenticationService
Mise à JOUR Mon Karma.conf :
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
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/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 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,
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
Et de ma constante définie dans un bootstrap.js fichier de l'instancier avant l'application démarre :
'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']);
});
});
});
});
Connaissez-vous la solution pour que le Karma arrêter de penser que ma constante est un fournisseur?
La solution
Il appert que le document est prêt événement n'est jamais jeté dans votre jasmin test et, par conséquent, la constante n'est pas définie dans votre App
le module.Essayer de se moquer de CSRF_TOKEN
constant dans votre module de section de configuration de test:
JavaScript
describe('Test', function() {
beforeEach(function() {
module('App', function($provide) {
$provide.constant('CSRF_TOKEN', 'MOCK_CONSTANT'); // <= mock your constant
});
});
// Tests go here
});