长话短说,当我运行 karma.conf.js
我看到 Uncaught ReferenceError: angular is not defined
我开始使用一个预先存在的 Ionic 项目..并添加一些单元测试。我需要查看我们的 www/index.html
吗? .. 我没看到angular.js
包括在内很奇怪..这是原因吗?
这个团队使用 Maven 进行构建,所以我担心我们在构建方面与社区的其他成员不一致。
更新 1:添加 <script type="text/javascript" src="lib/js/angular/angular.js"></script>
在 www/index.html
的顶部的包含无效。
更新 2:index.html
包含 ionic 束
<script src="lib/js/ionic.bundle.js"></script>
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 已经有了 Angular,所以你不需要在外部添加 Angular。你应该删除 <script type="text/javascript" src="lib/js/angular/angular.js"></script>
并确保您在依赖项中添加了 Ionic
angular.module('myApp', ['ionic'])
并且:记得在您的 index.html
中执行此操作
<body ng-app="myApp"></body>
*可能在 body
中标签或 html
标记,只需确保您正在添加它。
编辑
正如@PSL 所说:Ionic Bundle 中包含 Angular。所以我想,您应该开始使用 Ionic Bundle 而不再担心它。
如果您不使用 Ionic Bundle,请在您的索引中添加对 Angular 的引用,并进行我上面提到的声明。
你的 karma 文件应该是这样的
// 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'
],
如果您没有使用上面的某些文件,只需将其从数组 files: []
中删除即可
关于javascript - 使用 Jasmine 和 Karma 测试 Ionic - 未捕获的 ReferenceError : angular is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356999/