Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Jasmine Test Fails: Failed to instantiate module

Tags:

My angular app worked great and so did my tests, using karma and jasmine, until I added a dependency in ui.bootstrap. Now the app still works as expected, but I can't get my tests to run. This is what I have:

app.js - added dependency in ui.bootstrap

angular.module('myApp', ['ngResource', 'ngRoute', 'ui.bootstrap']).config(function(...) {...}); 

service.js

angular.module('myApp').service('myService', function () {}) 

controller.js

angular.module('myApp').controller('MyController', function ($scope, $http, myService) {}) 

tests/main.js

describe('Controller: MyController', function () {     var MyController, scope;     // load the controller's module     beforeEach(function(){         module('myApp');         inject(function ($controller, $rootScope) {             scope = $rootScope.$new();             MyController = $controller('MyController', {                 $scope:scope             });         });     });     it('should do something', function () {         expect(scope.myOptions.length).toBe(5);     }); });  

And my test, which I run using grunt and krama, fails due to:

Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] Failed to instantiate module ui.bootstrap due to: Error: [$injector:nomod] Module 'ui.bootstrap' is not available! You either misspelled the module name or forgot 

What have I missed? The app runs with no problem, only the test fails.

like image 308
Haji Avatar asked Feb 10 '14 14:02

Haji


2 Answers

In karma.conf.js there is a list of files that karma loads before test execution:

// list of files / patterns to load in the browser files: [   'bower_components/angular/angular.js',   'bower_components/angular-mocks/angular-mocks.js',   ... ] 

Add bootstrap-ui.js there.

like image 68
Bastian Voigt Avatar answered Sep 21 '22 12:09

Bastian Voigt


Inject your dependencies

beforeEach(function(){    angular.module('ui.bootstrap',[]); }); 
like image 36
ndurgaprasad Avatar answered Sep 22 '22 12:09

ndurgaprasad