【问题标题】:AngularJS Jasmine Test Fails: Failed to instantiate moduleAngularJS Jasmine 测试失败:无法实例化模块
【发布时间】:2014-03-07 23:25:28
【问题描述】:

我的 Angular 应用运行良好,我的测试也运行良好,使用 karma 和 jasmine,直到我在 ui.bootstrap 中添加了一个依赖项。现在该应用程序仍按预期工作,但我无法运行我的测试。这就是我所拥有的:

app.js - 在 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);
    });
}); 

我使用 grunt 和 krama 运行的测试失败,原因是:

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

我错过了什么?应用程序运行没有问题,只有测试失败。

【问题讨论】:

  • 请检查您的 karam.conf.js 文件是否包含 bootstrap-ui js 文件。
  • 是的,就是这样。错过了那里的 karma.conf.js。谢谢!
  • 我刚才也遇到了同样的问题,但我想知道如果它是一个单元测试,为什么我应该加载所有模块并且应该隔离那段特定的代码?除非我误解了,否则感觉就像您必须加载更像是集成测试的所有内容?

标签: javascript angularjs jasmine angular-ui-bootstrap karma-jasmine


【解决方案1】:

karma.conf.js 中有一个 karma 在测试执行之前加载的文件列表:

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

在此处添加 bootstrap-ui.js。

【讨论】:

  • 问题仍然显示为未回答,因为它仅在 cmets 中得到回答。所以我再次添加它作为“真正的答案”
  • 问题:你不需要做这样的事情吗? module('myApp', ['ngResource', 'ngRoute', 'ui.bootstrap']) 在测试中引用并实例化模块?
【解决方案2】:

注入你的依赖项

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

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。刚刚解决了。不知何故将module(myApp); 函数调用放在你提供给beforeEach() 的函数中是行不通的,试试这个:

    将模块调用提取到它自己的 beforeEach() 中:

    beforeEach(module('myApp'));
    

    并为您使用的函数使用另一个 beforeEach()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多