AngularJS Jasmine 测试失败:无法实例化模块 [英] AngularJS Jasmine Test Fails: Failed to instantiate module

查看:25
本文介绍了AngularJS Jasmine 测试失败:无法实例化模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 - 在 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 运行的测试失败,原因是:

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.

推荐答案

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

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',
  ...
]

在那里添加 bootstrap-ui.js.

Add bootstrap-ui.js there.

这篇关于AngularJS Jasmine 测试失败:无法实例化模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆