gpt4 book ai didi

spock - Spock 特征方法中先决条件的好风格是什么?

转载 作者:行者123 更新时间:2023-12-04 11:51:24 25 4
gpt4 key购买 nike

在特征方法中,在 when: 中指定特征 Action 。块,其结果在后续 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
}
}

对于这些先决条件,建议的做法是什么?我用过 assert在示例中,但很多人不赞成 assert s 在规范中。

最佳答案

我一直在用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
}
}

关于spock - Spock 特征方法中先决条件的好风格是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com