AngularJS Jasmine 测试失败:无法实例化模块

编程入门 行业动态 更新时间:2024-10-28 07:21:27
本文介绍了AngularJS Jasmine 测试失败:无法实例化模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 06:25:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/998278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   模块   测试   AngularJS   Jasmine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!