2016-01-11 13 views
6

Questa è la mia prima volta che uso Jasmine e ho testato la mia prima Factory senza problemi.Usa Jasmine per testare un servizio con uibModal e lodash come dipendenze

Ma ora, voglio provare questo servizio:

angular.module('Questions', []) 
.service('QuestionsService', function($uibModal, $log, _) { 
    ... 
} 

$ uibModal da UI Bootstrap (vedi here) e _ è Lodash.

mio Jasmine prova finora è:

describe('Service: QuestionsService', function() { 

    var QuestionsService; 

    beforeEach(inject(function(_QuestionsService_) { 
     QuestionsService = _QuestionsService_; 
    })); 

    ... 
} 

E quando ho provato (test grugnito), ottengo il seguente errore:

Error: [$injector:unpr] Unknown provider: $uibModalProvider <- $uibModal <- QuestionsService

E a un certo punto ho avuto anche:

Error: [$injector:unpr] Unknown provider: _Provider <- _ <- QuestionsService

Se può aiutare, il mio Karma conf è:

module.exports = function(config) { 
    'use strict'; 
    config.set({ 
    autoWatch: true, 
    basePath: '../', 

    frameworks: [ 
     "jasmine" 
    ], 

    // list of files/patterns to load in the browser 
    files: [ 
     // bower:js 
     'bower_components/jquery/dist/jquery.js', 
     'bower_components/lodash/lodash.js', 
     'bower_components/angular/angular.js', 
     'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js', 
     'bower_components/angular-animate/angular-animate.js', 
     'bower_components/angular-cookies/angular-cookies.js', 
     'bower_components/angular-resource/angular-resource.js', 
     'bower_components/angular-route/angular-route.js', 
     'bower_components/angular-sanitize/angular-sanitize.js', 
     'bower_components/angular-touch/angular-touch.js', 
     'bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 
     'bower_components/angular-mocks/angular-mocks.js', 
     // endbower 
     "app/scripts/**/*.js", 
     "test/mock/**/*.js", 
     "test/spec/**/*.js", 
    ], 
    exclude: [ 
    ], 
    port: 8080, 
    browsers: [ 
     "PhantomJS" 
    ], 
    plugins: [ 
     "karma-phantomjs-launcher", 
     "karma-jasmine" 
    ], 
    singleRun: false, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    }); 
}; 
+0

Nel file di configurazione karma, in cui sono stati definiti i file da includere, definire esplicitamente l'inizializzazione dell'applicazione ('app.js') dopo le dipendenze bower, ma prima del resto dei file. Quindi andrebbe a destra prima di "app/scripts/**/*. Js" '. È possibile che tu abbia potuto scrivere nuovi file che Karma aggiunge prima del file di avvio dell'applicazione. – Healforgreen

+1

Grazie per il vostro aiuto. Ho provato ad aggiungere la mia "app/scripts/app.js" prima di "app/scripts/**/*. Js", ma il problema è rimasto. – didjoman

+1

E il tuo 'app.js' include Lodash e UI-Bootstrap? 'angular.module ('app', ['underscore', 'ui.bootstrap']);' – Healforgreen

risposta

0

Il modulo dell'app non è stato incluso nel test. Il test refactoring per il QuestionService sarebbe:

describe('Service: QuestionsService', function() { 

    var QuestionsService; 

    // The module needs to be included in the test. 
    beforeEach(module('boardgameApp')); 

    beforeEach(inject(function(_QuestionsService_) { 
     QuestionsService = _QuestionsService_; 
    })); 

    ... 
} 
1

Solo nel caso in altri trovare questo. Per risolvere l'errore quando si verifica il controllore di una direttiva, ho preso in giro il servizio $ uibModal, concettualmente simile a questo:

describe('Service: QuestionsService', function() { 

    var controller; 

    beforeEach(inject(function($controller) { 
     controller = $controller('controllerName', { 
      $uibModal : {} 
     }); 
    })); 

    ... 
} 

$ uibModal può avere bisogno di essere più di un semplice oggetto vuoto se si sta scrivendo i test contro le funzioni della centralina che interagiscono con esso.

Problemi correlati