Spock特征方法的前提条件有什么好的风格?

Tam*_*más 7 spock

在特征方法中,指定when:块中的特征动作,其结果在后续then:块中进行测试.通常需要准备,这在一个given:条款(setup:或夹具方法)中完成.包含前提条件同样有用:这些条件不是特征测试的主题(因此不应该在when:- then:expect:),但它们断言/记录测试有意义的必要条件.例如,参见下面的虚拟规范:

import spock.lang.*

class DummySpec extends Specification {
  def "The leading three-caracter substring can be extracted from a string"() {
    given: "A string which is at least three characters long"
    def testString = "Hello, World"

    assert testString.size() > 2

    when: "Applying the appropriate [0..2] operation"
    def result = testString[0..2]

    then: "The result is three characters long"
    result.size() == 3
  }
}
Run Code Online (Sandbox Code Playgroud)

这些先决条件的建议做法是什么?我assert在示例中使用了但很多人对assert规范中的s 皱眉.

Opa*_*pal 7

我一直在使用expect这种场景:

@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class DummySpec extends Specification {
  def "The leading three-caracter substring can be extracted from a string"() {
    given: "A string which is at least three characters long"
    def testString = "Hello, World"

    expect: "Input should have at least 3 characters"
    testString.size() > 3

    when: "Applying the appropriate [0..2] operation"
    def result = testString[0..2]

    then: "The result is three characters long"
    result.size() == 3
  }
}
Run Code Online (Sandbox Code Playgroud)