javascript - 使用 Jasmine 和 Karma 测试 Ionic - 未捕获的 ReferenceError : angular is not defined

标签 javascript angularjs ionic-framework karma-jasmine

长话短说,当我运行 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/

相关文章:

javascript - 确定 razor 生成 View 中 html 表的大小

javascript - 为内部 html 创建具有 id 属性但 id 仍在外部 html 上的 angularjs 指令

javascript - 如何捕获在 HTML 中调用的 onclick 事件?

javascript - 卡在第二个 if 语句 javascript 中

javascript - Angularjs View 不更新,单击按钮时 View 闪烁

javascript - 如何使用 AngularJS 将 JSON 树递归渲染为 HTML?

angularjs - 在 Neo4j 新版本中是否可以在没有插件的情况下压缩 REST API 响应 gzip?

android - sqlite3_step失败: NOT NULL constraint failed

angularjs - 将 Mailgun 与 Ionic 集成

ios - Ionic 4准备iOS发布时如何处理console.log?