AngularJS Jasmine Test Fails: Failed to instantiate module

梦想的初衷 提交于 2019-12-04 16:37:55

问题


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.


回答1:


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.




回答2:


Inject your dependencies

beforeEach(function(){
   angular.module('ui.bootstrap',[]);
});



回答3:


I had the same problem. Just solved it. Somehow putting the module(myApp); function call inside a the function you provide to beforeEach() doesn't work just try this:

Extract the module call into its own beforeEach():

beforeEach(module('myApp'));

And use another beforeEach() for the function you use.



来源:https://stackoverflow.com/questions/21680455/angularjs-jasmine-test-fails-failed-to-instantiate-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!