简体   繁体   English

不能用karma,jasmine,phantomjs和maven加载angular.js进行单元测试

[英]can't load angular.js for unit test with karma, jasmine, phantomjs, and maven

I'm trying to arrange my alphabet soup of angular, karma, jasmine, phantomjs, and maven. 我正在尝试安排我的角蛋,业力,茉莉,幻影和maven的字母汤。 I'm doing this with a toy project for now, in preparation for integrating into a real project. 我现在正在做一个玩具项目,准备整合到一个真实的项目中。

The following is my relevant list of files: 以下是我的相关文件列表:

./pom.xml
./src/main/webapp/index.html
./src/main/webapp/js/blog.js
./src/test/webapp/js/blogSpec.js
./src/test/webapp/js/karma.conf.js
./src/test/webapp/js/libs/angular-mocks.js
./src/test/webapp/js/libs/angular.js

Here is my complete pom: 这是我完整的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.att.cdits</groupId>
  <artifactId>angularjsexp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>com.kelveden</groupId>
        <artifactId>maven-karma-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            <configFile>${basedir}/src/test/webapp/js/karma.conf.js</configFile>
            <browsers>PhantomJS</browsers>
            <reporters>dots</reporters>
            <colors>false</colors>
            <autoWatch>false</autoWatch>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>com.kelveden</groupId>
        <artifactId>maven-karma-plugin</artifactId>
        <version>1.5</version>
    </dependency>
  </dependencies>
</project>

And my "karma.conf.js" file: 还有我的“karma.conf.js”文件:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      "../../../../src/test/webapp/js/libs/*.js",
      "../../../../src/main/webapp/js/*.js",
      "../../../../src/test/webapp/js/*.js"
    ],
    exclude: [],
    reporters: ['dots'],
    port: 9876,
    colors: false,
    logLevel: config.LOG_DEBUG,
    autoWatch: false,
    browsers: ['PhantomJS'],
    captureTimeout: 60000,
    singleRun: true
  });
};

The following is an excerpt from my "mvn install" output, starting at the Surefire execution: 以下是我的“mvn install”输出的摘录,从Surefire执行开始:

[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ angularjsexp ---
[INFO] Surefire report directory: myhome\workspace6\angularjsexp\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-karma-plugin:1.5:start (default) @ angularjsexp ---
cmd /C karma start myhome\workspace6\angularjsexp\src\test\webapp\js\karma.conf.js --browsers PhantomJS --reporters dots --single-run --no-auto-watch --colors false
DEBUG [plugin]: Loading karma-* from myhome\AppData\Roaming\npm\node_modules 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-chrome-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-coffee-preprocessor. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-firefox-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-html2js-preprocessor. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-jasmine. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-phantomjs-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-requirejs. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-script-launcher. 
DEBUG [plugin]: Loading inlined plugin (defining ). 
INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/ 
INFO [launcher]: Starting browser PhantomJS 
DEBUG [launcher]: Creating temp dir at C:\Cygwin\tmp\karma-77302282 
DEBUG [launcher]: myhome\AppData\Roaming\npm\node_modules\karma-phantomjs-launcher\node_modules\phantomjs\lib\phantom\phantomjs.exe C:\Cygwin\tmp\karma-77302282/capture.js 
DEBUG [watcher]: Excluded file "myhome/workspace6/angularjsexp/src/test/webapp/js/karma.conf.js" 
DEBUG [watcher]: Resolved files: 
    myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js 
    myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/blog.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/client.html 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/karma.js 
DEBUG [karma]: A browser has connected on socket LI1NDCsuY-0i0Uxwkhs_ 
INFO [PhantomJS 1.9.2 (Windows 7)]: Connected on socket LI1NDCsuY-0i0Uxwkhs_ 
DEBUG [karma]: All browsers are ready, executing 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/context.html 
DEBUG [web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js 
DEBUG [web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/blog.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: angular 
    at myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js:16 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: myApp 
    at myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js:1 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: myApp 
    at myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js:1 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: module 
    at myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js:5 
PhantomJS 1.9.2 (Windows 7): Executed 0 of 0 ERROR (0.273 secs / 0 secs) 

I have the following issues so far, some annoyances, some blockers: * It's weird that karma resolves file path references relative to where it found my conf file, I wish I could override that. 到目前为止,我有以下问题,一些烦恼,一些阻止者:* karma解析相对于它找到我的conf文件的位置的文件路径引用是奇怪的,我希望我可以覆盖它。 * It appears that Surefire fires before Karma. *似乎Surefire在Karma之前开火。 Is it possible to integrate these? 是否可以整合这些? * My references to "angular" and other objects in my scripts are obviously failing. *我在脚本中对“angular”和其他对象的引用显然是失败的。 I don't understand how to resolve this. 我不明白如何解决这个问题。

Thanks to someone on the karma-users discussion group, I discovered that the files loaded from "files" have to be loaded in the correct order. 感谢karma-users讨论组中的某个人,我发现从“文件”加载的文件必须以正确的顺序加载。 For instance, instead of " ../../../../src/test/webapp/js/libs/*.js ", I need to specify " ../../../../src/test/webapp/js/libs/angular.js ", then " ../../../../src/test/webapp/js/libs/angular-mocks.js ". 例如,代替“ ../../../../src/test/webapp/js/libs/*.js ”,我需要指定“ ../../../../src/test/webapp/js/libs/angular.js “,然后” ../../../../src/test/webapp/js/libs/angular-mocks.js “。

I still have to figure out how to properly integrate Surefire, but I'll ask that separately. 我仍然需要弄清楚如何正确集成Surefire,但我会单独询问。

I have been testing Angular with Karma and Jasmine and finally it works! 我一直用Karma和Jasmine测试Angular,最后它起作用了! My example is much simpler than yours, but maybe it can help you. 我的例子比你的简单得多,但也许它可以帮到你。 I have an application in Angular in two files: 我在Angular中有两个文件中的应用程序:

  • controllers.js
  • application.js

And the following files for Angular: 以及Angular的以下文件:

  • angular.js
  • angular-mocks.js
  • angular-route.js

I have edited the karma.config.js manually and I have put files instead of routes because it works in a concrete order, so finally the ordered file list for Karma is: 我手动编辑了karma.config.js并且我放了文件而不是路由,因为它按照具体顺序工作,所以最后Karma的有序文件列表是:

// list of files / patterns to load in the browser
files: [
  'js/libs/angular.js',
  'js/libs/angular-mocks.js',
  'js/libs/angular-route.js',
  'js/controllers.js',
  'js/application.js',
  'spec/*.spec.js'
],

// list of files to exclude
exclude: [
  'karma.config.js'
],

My directory tree is: 我的目录树是:

  • karma.config.js karma.config.js
  • /js / JS
    • application.js 的application.js
    • controllers.js controllers.js
    • /libs /库
      • angular.js angular.js
      • angular-mocks.js 角mocks.js
      • angular-route.js 角route.js
  • /spec /规格
    • application.spec.js application.spec.js

And I test Karma in the root folder with $ karma start karma.config.js . 我用$ karma start karma.config.js在根文件夹中测试Karma。

Hope this helps 希望这可以帮助

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

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