Angular App运行良好,但ngMock没有正确加载模块

时间:2016-06-07 18:45:28

标签: javascript angularjs unit-testing ngmock

我的应用程序在部署并在浏览器中运行时运行正常

我可以成功覆盖所有控制器/服务。它们通过子模块链接在一起。

我使用子模块为ui-router更好地组织状态/路由。 (之前我们在app dot js文件中有所有状态。有50个。现在它们被组织成更小的模块。)

app.js [模块文件]

var phpApp = angular.module("phpApp",
[
    "ui-router"
    "phpApp.components" , ...

]).config(...).run(...);

components.js [模块文件,包含州/路线]

var ComponentModule = angular.module("phpApp.components",
[
    "ui-router"
    "phpApp.components.user" , 
    "phpApp.components.client"
     ...

]).config(...).run(...);

user.js [模块文件,包含州/路线]

var UserModule = angular.module("phpApp.components.user",
[
    "ui-router"

]).config(...).run(...);

user-controller.js // LOADS罚款;是一个控制者。

angular.module('phpApp.components').controller('UserController', ....);

client-controller.js // 加载!找不到'phpApp.components'模块......

angular.module('phpApp.components').controller('ClientController', ...);

我的index.html文件按以下顺序加载脚本:

<script src="app.js"></script>
<script src="components/components-module.js"></script>
<script src="components/user/user-module.js"></script>
<script src="components/client/client-module.js"></script>
...
<script src="components/user/user-controller.js"></script>
<script src="components/client/**client-controller.js**"></script>

我不明白。我的客户端控制器在我的用户控制器之后加载......但它无法找到合适的模块?

我收到错误:

Uncaught Error: [$injector:nomod] Module 'phpApp.components' 
is not available! You either misspelled the module name or 
forgot to load it.

此时我非常困惑和沮丧。我不确定我哪里出错了。

1 个答案:

答案 0 :(得分:0)

答案在于karma配置文件(默认情况下为karma.conf.js)

正如预期的那样,答案很简单。

我有一条规则要包含所有JavaScript文件:

"app/app.js",
"app/components/**/*.js",

您需要确保重写加载规则以首先加载所有模块JS文件:

"app/app.js",
"app/components/**/*-module.js", <-- modules must load first
"app/components/**/*.js",

为了帮助解释,我的项目设置在:

1) controllers are in their respective xxx-controller.js files, 
2) ervices are in xxx-service.js files and 
3) modules are in xxx-module.js files