jasmine-maven-plugin + phantomJS:错误:[$ injector:nomod]

时间:2015-02-26 08:32:50

标签: angularjs maven phantomjs jasmine-maven-plugin

我创建了一个项目:

mvn archetype:generate -DarchetypeGroupId=com.github.searls -DarchetypeArtifactId=jasmine-archetype -DarchetypeVersion=1.3.1.5 -DgroupId=com.acme -DartifactId=my-jasmine-project -Dversion=0.0.1-SNAPSHOT

首先我用 mvn jasmine进行测试:测试测试正常运行。

注意:我使用maven 3.2.1

在"目标"目录我添加了一个文件夹" phantomjs-1.9.7-windows"我放在哪里" phantomjs.exe"

我修改了我原来的pom.xml来添加

                ...
    <configuration>
        <srcDirectoryName>${project.artifactId}/src/main/javascript</srcDirectoryName>
        <webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
        <webDriverCapabilities>
            <capability>
                <name>phantomjs.binary.path</name>
                <value>${project.build.directory}/phantomjs-1.9.7-${os.phantomJS}/${run.phantomJS}</value>
            </capability>
        </webDriverCapabilities>


        <preloadSources>
            <source>${project.basedir}/src/main/javascript/libs/jquery-1.9.1.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular-ui-router.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular-resource.js</source>
        </preloadSources>
    </configuration>
    ...

我创建了一个文件夹&#34; libs&#34;在&#34; src / main / javascript&#34;我把它放进去: - jquery-1.9.1.js - angular.js - angular-ui-router.js - angular-resource.js

注意:角度为1.2.23

之后我创建了另一个文件夹&#34; app&#34;在&#34; src / main / javascript&#34;我把它放进去了:&#34; app.module.js&#34;

(function() {
    'use strict';

    angular.module('app', [
         'app.Security.modules'
    ]);
})();

在&#34; src / main / javascript / app&#34;我创建了一个文件夹&#34; security&#34;我把它放进去了:&#34; security.module.js&#34;

(function() {
'use strict';
    angular.module('app.Security.modules', []);
})();

和&#34; cha_directive.js&#34;

angular.module('app.Security.modules').directive('inputOld', InputAccessKeyManagement);
function InputAccessKeyManagement () {}

如果我使用 mvn jasmine:test 进行测试,我会收到错误消息:

[ERROR - 2015-02-26T08:12:49.751Z] Session [40eacc20-bd8f-11e4-a736-9934c466a5ff] - page.onError - stack:
  (anonymous function) (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1679)
  ensure (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1601)
  module (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1892)
  (anonymous function) (http://localhost:51170/jasmine-project/src/main/javascript/app/security/cha_directive.js:1)
[WARNING] JavaScript Console Errors:

  * Error: [$injector:nomod] Module 'app.Security.modules' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.23/$injector/nomod?p0=app.Security.modules


[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.163s
[INFO] Finished at: Thu Feb 26 09:12:49 CET 2015
[INFO] Final Memory: 12M/210M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.searls:jasmine-maven-plugin:1.3.1.5:test (default-cli) on project jasmine-project: The jasmine-maven-plugin encountered an exception:
[ERROR] java.lang.RuntimeException: java.lang.RuntimeException: There were javascript console errors.
[ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:46)
[ERROR] at com.github.searls.jasmine.mojo.TestMojo.executeSpecs(TestMojo.java:86)

如果我只是重命名&#34; cha_directive.js&#34; in &#34; toto_directive.js&#34; (使用相同的源代码)我再次运行 mvn jasmine:test ,测试正常运行。

如果我使用&#34; cha_directive.js&#34;命名文件并运行 mvn jasmine:bdd 。之后,我在FF中运行URL http://localhost:8234,测试正常运行。

唯一的事情(如果我编辑&#34的HTML代码; Jasmine Spec Runner&#34;使用fireBug)是我可以看到这个标签:

<head jmp_jserror="Error: [$injector:nomod] Module 'app.Security.modules' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.2.23/$injector/nomod?p0=app.Security.modules">

如果我在&#34; checkForConsoleErrors&#34;中查看类SpecRunnerExecutor.java有一个测试:

WebElement head = driver.findElement(By.tagName("head"));
if (head != null) {
    String jserrors = head.getAttribute("jmp_jserror");
    if (StringUtils.isNotBlank(jserrors)) {
        throw new RuntimeException("There were javascript console errors.");
    }
}

注意:其他文件名会导致测试崩溃:input_directive.js,accessKey_directive.js,...

2 个答案:

答案 0 :(得分:1)

这是导入脚本顺序的问题。

我宣布

(function() {
   'use strict';
   angular.module('app.Security.modules', []);
})();

在名为&#34的文件中; s ecurity.module.js&#34;

我尝试使用我的声明

angular.module('app.Security.modules')
  .directive('inputOld', InputAccessKeyManagement);
  function InputAccessKeyManagement () {}

在名为&#34的文件中; c ha_directive.js&#34;

如果您没有指定自定义导入脚本顺序,则默认值为字母顺序,然后是&#34; c ha_directive.js&#34;在&#34; s ecurity.module.js&#34;之前。

我使用&#34; customRunnerTemplate&#34;。

解决了这个问题

但也许还有另一种方法可以做得更漂亮&#34; ?

答案 1 :(得分:1)

如果有其他人遇到此问题,一个好的解决方案是在sourceInclude块中执行类似的操作:

<sourceInclude>
  <include>**/*.module.js</include>
  <include>**/*.controller.js</include>
  <include>**/*.service.js</include>
...

等等。假设你遵守在一个名为“例如.module.js&#39;”的文件中定义的模块。首先加载模块,然后加载控制器等等