5

I'm new to maven and jasmine, and I have a problem setting up maven to run unit tests on an angular application. I get into trouble as soon as I add angular-mocks.js to the test/javascript/lib folder.

mvn test throws a htmlunit.ScriptException just by adding the angular-mocks file. If I remove the file, it runs just fine (without the parts relying on mocking of course, but they fail as a unit test rather than throwing an error).

Does anyone know what could cause this error and how to fix it?


EDIT
I stronly suspect that this is a htmlunit problem, because the error differs when changing the <browserVersion>:

CHROME gives:

TypeError: Cannot find function attachEvent in object [object HTMLDocument]. (http://localhost:55408/src/lib/angular.js#1568)

FIREFOX_3_6 gives:

Exception invoking Node.removeEventListener() with arguments [String, NativeArray, Boolean]

INTERNET_EXPLORER_9 gives:

Exception invoking Node.detachEvent() with arguments [String, NativeArray]


Stack trace (excerpt FF):

[ERROR] Failed to execute goal com.github.searls:jasmine-maven-plugin:1.3.1.2:test (default) on project my-jasmine-project: The jasmine-maven-plugin encountered an exception:
[ERROR] java.lang.RuntimeException: org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking Node.removeEventListener() with arguments [String, NativeArray, Boolean]
[ERROR] Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
[ERROR] System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.4', java.version: '1.6.0_51'
[ERROR] Driver info: driver.version: HtmlUnitDriver
[ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:39)
...

pom.xml (excerpt):

<plugin>
  <groupId>com.github.searls</groupId>
  <artifactId>jasmine-maven-plugin</artifactId>
  <version>1.3.1.2</version>
  <configuration>
    <preloadSources>
      <source>lib/angular.js</source>
      <!-- <source>lib/angular-mocks.js</source> -->
      <source>http://code.angularjs.org/1.1.5/angular-mocks.js</source>
    </preloadSources>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Both adding angular-mocks.js to the test/javascript/lib folder and referencing it directly from the angularjs website give the same error.

4

1 回答 1

8

在 CHROME 浏览器版本中,当 Angular 尝试加载 JQLite 一个 jQuery 库的 api 兼容子集时会发生错误,该 jQuery 库允许 Angular 操作 DOM。我能够通过下载 jQuery (v1.10.2)、angular (v1.0.7) 和 angular-mocks (v1.0.7) 的未压缩版本来解决这个问题。我在 jasmine-maven-plugin 的配置中添加了以下内容。

<preloadSources>
  <preloadSource>${angular-test-deps}/jQuery.js</preloadSource>
  <preloadSource>${angular-test-deps}/angular.js</preloadSource>
  <preloadSource>${angular-test-deps}/angular-mocks.js</preloadSource>
</preloadSources>

角度文档指出“Real jQuery 始终优先于 jqLit​​e,前提是它是在DOMContentLoaded事件触发之前加载的”(未压缩 1.0.7 的第 1448 行)。此修复在 CHROME 浏览器版本中仍然失败,但适用于 FIREFOX_3_6、FIREFOX_17、INTERNET_EXPLORER_9、INTERNET_EXPLORER_8 和 INTERNET_EXPLORER_7。jasmine-maven-plugin 默认为 FIREFOX_3_6 浏览器版本,因此无需指定浏览器版本。

于 2013-07-27T14:05:03.700 回答