测试手动引导的Angularjs应用程序 [英] Testing Angularjs application that are bootstrap manually

查看:117
本文介绍了测试手动引导的Angularjs应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些http ajax请求后在代码中手动执行bootstrap应用程序时测试angularjs应用程序(karma,jasmine)的问题。

i have problem with testing angularjs application (karma, jasmine) when bootstrap application manually in code after some http ajax requests.

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

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

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

I also have following code in my jasmine specs

beforeEach(module('app'));

当我运行我的规格时,我总是有同样的错误

And when i run my specs i always have the same error

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函数中的angular bootstrap应用程序?

Is there a way for waiting for angular bootstrap application in beforeEach function?

推荐答案

我认为这个问题是由于你的引导代码包含在karma配置中引起的。我遇到了同样的问题。

I believe this problem is caused by having your bootstrap code included in the karma config. I was having the same issue.

我拿了引导代码:

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

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

and moved it into its own js file called bootstrap.js. Then in my index.html I put the script tag for this at the bottom of the page (after the script where I declare my app module).

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

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

I made sure that bootstrap.js was not included in my karma config and now my tests pass.

业力失败的原因是因为它不需要运行引导代码,并且引导代码由业力运行,因为它无法初始化应用程序。

The reason karma was failing was because it does not need the bootstrap code to run and the bootstrap code was getting run by karma in way that it fails to initialize the app.

这篇关于测试手动引导的Angularjs应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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