4

我正在尝试安排我的 angular、karma、jasmine、phantomjs 和 maven 字母汤。我现在正在做一个玩具项目,准备集成到一个真实的项目中。

以下是我的相关文件列表:

./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

这是我完整的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>

还有我的“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
  });
};

以下是我的“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) 

到目前为止,我有以下问题,一些烦恼,一些阻碍: * 很奇怪,业力解析文件路径引用相对于它找到我的 conf 文件的位置,我希望我可以覆盖它。* 看来 Surefire 在 Karma 之前开火。有没有可能整合这些?* 我在脚本中对“角度”和其他对象的引用显然失败了。我不明白如何解决这个问题。

4

2 回答 2

12

感谢 karma-users 讨论组中的某个人,我发现从“文件”加载的文件必须以正确的顺序加载。例如../../../../src/test/webapp/js/libs/*.js,我需要指定“ ”而不是“ ../../../../src/test/webapp/js/libs/angular.js”,然后指定“ ../../../../src/test/webapp/js/libs/angular-mocks.js”。

我仍然需要弄清楚如何正确集成 Surefire,但我会单独询问。

于 2013-10-02T21:54:12.653 回答
7

我一直在用 Karma 和 Jasmine 测试 Angular,终于成功了!我的例子比你的简单得多,但也许它可以帮助你。我在 Angular 中有两个文件中的应用程序:

  • controllers.js
  • application.js

以及 Angular 的以下文件:

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

我已经手动编辑了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'
],

我的目录树是:

  • 业力.config.js
  • /js
    • 应用程序.js
    • 控制器.js
    • /库
      • 角.js
      • 角模拟.js
      • 角路由.js
  • /规格
    • application.spec.js

我在根文件夹中使用$ karma start karma.config.js.

希望这可以帮助

于 2014-02-06T16:18:01.927 回答