使用EasyJ中的Easyb或Spock,在Grails项目中“稍微测试一下,编码一点”

时间:2012-05-28 12:34:02

标签: grails intellij-idea bdd spock easyb

我的原始问题由this post完美描述:我想关注TDD:

  • 写一个小测试
  • 观看它失败
  • 编写足够的代码以使其成功
  • 看着它成功
  • 重复

我正在使用IntelliJ中的Grails项目。如果我想要的只是编写正常的JUnit测试,上面的帖子将解决所有问题:

  • 前往/ test / unit
  • 将一些测试代码放在“类Xyz extends GroovyTestCase”类
  • 点击Shift F10
  • JUnit报告会在一两秒内弹出

问题在于我想使用一种非常酷的“英语描述”测试设置,如Easyb或Spock。

我该怎么办?从Grails为我做的自动生成的Test类开始,然后将Spock塞入其中,这将是一件神奇的事。显然我不能两次使用“延伸”。这是否给出了我想要做的事情的要点?

class Xyz extends GroovyTestCase extends spock.lang.Specification {

    //void testSomething() {
    //    fail "Implement me"
    //}

    def "length of Spock's and his friends' names"() {
        expect:
        name.size() == length

        where:
        name     | length
        "Spock"  | 5
        "Kirk"   | 4
        "Scotty" | 6
    }
}

2 个答案:

答案 0 :(得分:3)

扩展spock类,而不是groovy。您可以从列出的in source code中选择UnitSpec,ControllerSpec,IntegrationSpec和其他。 Spock将负责其余部分。

答案 1 :(得分:1)

- 使用“grails install-templates”

- 将测试模板改为:

@artifact.package@

import grails.test.mixin.*
import org.junit.*
import spock.lang.Specification

@TestFor(@artifact.testclass@)
class @artifact.name@ extends Specification {

}

- 在Spock中编写测试代码(或普通的JUnit代码)

-IntelliJ-&gt;运行 - &gt;编辑配置。添加JUnit类型的新配置。测试种类:<All in package/All in UnitTest directory/All in one UnitTest class/etc.&gt;

- (快捷键:方法名称上的光标,或类名 - > ctrl-shift-F10)


关于我原来的问题: 回想起来,我对这篇博客文章中的“在目录中运行”部分感到困惑。当前的IntelliJ DOES允许您在目录或整个项目中运行所有JUnit测试。

一旦我理解了,下一步就是要意识到Spock测试是JUnit测试。在test / unit目录中创建一个“class Xyz extends Specification {}”类,用Spock代码填充它,然后运行为... JUnit。魔术!