Use Jasmine to test a service using uibModal and lodash as dependencies

This is my first time using Jasmine, and I tested my first Factory without any problems.

But now I want to test this service:

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

$ uibModal is from UI Bootstrap (see here ) and _ is Lodash.

My Jasmine test so far:

 describe('Service: QuestionsService', function() { var QuestionsService; beforeEach(inject(function(_QuestionsService_) { QuestionsService = _QuestionsService_; })); ... } 

And when I try (grunt test), I get the following error:

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

And at some point I also had:

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

If this can help, my Karma config:

 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, }); }; 
+6
source share
2 answers

The application module was not included in the test. The refactored test for QuestionService will be:

 describe('Service: QuestionsService', function() { var QuestionsService; // The module needs to be included in the test. beforeEach(module('boardgameApp')); beforeEach(inject(function(_QuestionsService_) { QuestionsService = _QuestionsService_; })); ... } 
0
source

Just in case, others will find it. To solve this error while testing the controller, I mocked the $ uibModal service, conceptually as follows:

 describe('Service: QuestionsService', function() { var controller; beforeEach(inject(function($controller) { controller = $controller('controllerName', { $uibModal : {} }); })); ... } 

$ uibModal may take more than just an empty object if you write tests against controller functions that interact with it.

+1
source

All Articles