【问题标题】:Testing Angularjs application that are bootstrap manually测试手动引导的 Angularjs 应用程序
【发布时间】:2015-09-17 05:49:58
【问题描述】:

在一些 http ajax 请求后,在代码中手动引导应用程序时,我在测试 angularjs 应用程序(业力、茉莉花)时遇到问题。

angular.module("app", []);

angular.element(document).ready(function() {
   angular.bootstrap(document, ["app"]);
});

我的茉莉花规格中也有以下代码

beforeEach(module('app'));

当我运行我的规范时,我总是遇到同样的错误

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

有没有办法在 beforeEach 函数中等待角度引导应用程序?

【问题讨论】:

  • 你的 Karma 配置文件中确实加载了该模块吗?手动引导对单元测试没有影响。

标签: javascript angularjs jasmine karma-runner


【解决方案1】:

我相信这个问题是由于你的引导代码包含在 karma 配置中造成的。我遇到了同样的问题。

我拿了引导代码:

angular.element(document).ready(function() {
   angular.bootstrap(document, ["app"]);
});

并将其移动到自己的名为 bootstrap.js 的 js 文件中。然后在我的 index.html 中,我将它的脚本标记放在页面底部(在我声明我的应用程序模块的脚本之后)。

<script src="bootstrap.js"></script>

我确保 bootstrap.js 没有包含在我的 karma 配置中,现在我的测试通过了。

karma 失败的原因是它不需要运行引导代码,并且引导代码被 karma 以无法初始化应用程序的方式运行。

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2015-12-06
    • 2017-07-09
    相关资源
    最近更新 更多