【问题标题】:jasmine not loading js files from the 'src' directory茉莉花没有从'src'目录加载js文件
【发布时间】:2012-07-04 08:35:10
【问题描述】:

我已经成功构建了我的 maven 项目,该项目使用 jasmine-maven-plugin 将源代码和测试 javascript 文件放在正确的位置。当我有一个简单的测试时,例如:

describe('true', function() {
    it('should be true', function() {
        expect(true).toBe(true);
    })
})

整个过程没有问题,所有茉莉花规格都通过了。但是,当我尝试创建我在包含 target/jasmine/src 文件夹的文件之一中概述的对象的实例时,我收到“ReferenceError:“Stat”未定义”错误。

describe('stat test',function() {
    var stat = new Stat();

    it('get data',function() {
        stat.data = 13;
        expect(stat.getData()).toBe(13);
    });
});

js 文件是否加载不正确?完全被这里难住了。

【问题讨论】:

    标签: javascript maven jasmine


    【解决方案1】:

    茉莉花的设置是否正确? jasmine好像找不到你的js文件,这里有一个maven配置的例子:

    <plugin>
            <groupId>com.github.searls</groupId>
            <artifactId>jasmine-maven-plugin</artifactId>
            <version>1.1.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal> test </goal>
                    </goals>
                </execution>
             </executions>
             <configuration>
                 <jsSrcDir> main/www/js </jsSrcDir>    <---HERE you define the source directory
                 <haltOnFailure>false</haltOnFailure>
                 <sourceIncludes>       <---- HERE you specifies which files you include into the test
                    <include>libs/jquery-1.7.1.js</include>
                    <include>libs/underscore.js</include>
                    <include>**/*.js</include>
                 </sourceIncludes>
                 <sourceExcludes> <----- HERE you define the files that you exclude
                      <exclude>jsonresponses-mock.js</exclude>
                      <exclude>libs/jquery.mockjax.js</exclude>
                 </sourceExcludes>
                 <jsTestSrcDir> test/www/fakeJs </jsTestSrcDir> <---Define your Test source Dir
                       <haltOnFailure>true</haltOnFailure>
                       <browserVersion>FIREFOX_3</browserVersion>
                       <serverPort>8234</serverPort>
                       <specDirectoryName>specs</specDirectoryName>
             </configuration>
    </plugin>
    

    在这个页面的底部:http://searls.github.com/jasmine-maven-plugin 你有所有可能的标签。 检查您的 pom 是否以正确的方式使用...希望它可以提供帮助!

    【讨论】:

    • 是的,我的 pom.xml 看起来就像这样,除了我省略了 jsTestSrcDir 标记,因为它默认为我的测试所在的 src/test/javascript。当我运行“maven install”时,我会在文件夹“target/jasmine/src”中获得所有正确的 js 文件,它们应该在哪里。尽管如此,由于某种原因,它们没有从这里加载。
    • 你试过使用 require 吗?一个例子是: var Blog = require('../MyClass').MyClass;在您描述的顶部。在此链接中,您有一个示例:gregs.tcias.co.uk/2011/07/12/first-steps-with-jasmine
    • 是的,我也试过了。我得到'ReferenceError: "require" is not defined'。还有其他想法吗?感谢您的帮助,这让我疯狂了好几天!
    • mm 文件的顺序在 sourceIncludes 中很重要,首先放置您的库,然后将路径(来自 jsSrcDir)放置到您的 Stat 对象。
    • 啊!弄清楚了。源包含在之前定义的 中的根目录。现在似乎很明显,我已经解决了。
    【解决方案2】:

    经过很大的精神压力和运气不佳,我只是修改了插件以忽略感知到的 javascript 错误,以便一切都可以编译。瞧,这一切都奏效了!脚本只是乱序添加。对于那些感兴趣的人,我在 TestMojo.java 的第 90 行添加了“client.setThrowExceptionOnScriptError(false)”,所以现在当标签设置为 false(默认)时,javascript 错误将被忽略。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2020-05-07
      • 2015-05-05
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多