简体   繁体   English

使用Jasmine和Karma测试离子-未捕获的ReferenceError:角度未定义

[英]Testing Ionic with Jasmine and Karma - Uncaught ReferenceError: angular is not defined

Long story short, when I run karma.conf.js I see Uncaught ReferenceError: angular is not defined 长话短说,当我运行karma.conf.js我看到了Uncaught ReferenceError: angular is not defined

I am starting to work with a pre-existing Ionic project.. and add some unit tests. 我开始处理一个预先存在的Ionic项目。并添加一些单元测试。 Do I need to check our www/index.html .. I do not see angular.js included strangely enough.. is this the reason? 我是否需要检查我们的www/index.html ..我看angular.js足够奇怪的包含angular.js ..这是原因吗?

This team uses maven to build so I'm worried we're out of step with the rest of the community as far as building goes. 该团队使用maven进行构建,因此我担心在构建过程中,我们与社区其他成员的步调不一致。

Update 1: adding <script type="text/javascript" src="lib/js/angular/angular.js"></script> at the top of www/index.html 's includes has no effect. 更新1:在www/index.html包含的顶部添加<script type="text/javascript" src="lib/js/angular/angular.js"></script>无效。

Update 2: index.html contains ionic bundle <script src="lib/js/ionic.bundle.js"></script> 更新2: index.html包含离子束<script src="lib/js/ionic.bundle.js"></script>

Karma.conf.js Karma.conf.js

// list of files / patterns to load in the browser
files: [
    'www/lib/js/angular/angular.js',
    'www/lib/js/angular/angular-animate.js',
    'www/lib/js/angular/angular-animate.js',
    'www/lib/js/angular/angular-mocks.js',
    'www/lib/js/angular/angular-resource.js',
    'www/lib/js/angular/angular-sanitize.js',
    'www/lib/js/angular-ui/angular-ui-router.js',
    'www/lib/js/ionic.js',
    'www/lib/js/ionic-angular.js',
    'www/js/**/*.js',
    'www/test/jasmine/*.js'
],

Ionic has Angular already, so you don't need to add Angular externally. Ionic已经有了Angular,因此您无需在外部添加Angular。 You should probably remove <script type="text/javascript" src="lib/js/angular/angular.js"></script> 您可能应该删除<script type="text/javascript" src="lib/js/angular/angular.js"></script>

and make sure you are adding Ionic in your dependencies 并确保您在依赖项中添加了Ionic

angular.module('myApp', ['ionic'])

And: remember to do this in your index.html 并且:请记住在index.html执行此操作

<body ng-app="myApp"></body>

*Could be in body tag or html tag, just make sure you are adding it. *可以在body标签或html标签中,只需确保添加即可。

EDIT 编辑

as @PSL says: Ionic Bundle has Angular in it. 正如@PSL所说:Ionic Bundle中包含Angular。 So I guess, you should to start using Ionic Bundle and stop worrying about it. 所以我想,您应该开始使用Ionic Bundle,而不必担心它。

In case you are not using Ionic Bundle, add the reference for Angular in your Index, and make the declarations I mentioned above. 如果您不使用Ionic Bundle,请在索引中添加Angular的引用,并进行我上面提到的声明。

your karma file should look like this 您的业​​力档案应如下所示

// list of files / patterns to load in the browser
    files: [
    //Angular source
    'lib/ionic/js/ionic.bundle.js',
    'lib/angular-mocks/angular-mocks.js',
    'lib/angular-local-storage/dist/angular-local-storage.js',
    'lib/ngCordova/dist/ng-cordova.js',
    'lib/ionic/js/angular-ui/angular-ui-router.js',
    'lib/angular-animate/angular-animate.js',
    'lib/angular-sanitize/angular-sanitize.js',

    //App code
    'js/*.js',
    'js/controllers/*.js',
    'js/services/*.js',

    //Test files
    'test/controllers/*.js'
    ],

in case that you are not using some of the files above, just remove it from the array files: [] 如果您不使用上面的某些文件,只需将其从数组files: []删除files: []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM