在 Spock when: block 中的一行中声明多个变量
Declare multiple variables in one line in Spock when: block
我想知道如何在不初始化的情况下在 Spock 规范中的一行中定义多个变量,如下所示。
我试过:
import spock.lang.Specification
class exampleSpec extends Specification {
def "foo"() {
when:
def a, b
a = 0
b = 1
then:
a != b
}
}
但是访问b时失败了:
No such property: b for class: ....
我已经设法使以下工作正常进行:
def (a, b) = []
但我想要更好的。
非常感谢任何帮助!
我正在使用:
Groovy Version: 2.4.3 JVM: 1.8.0_45 Vendor: Oracle Corporation OS: Linux
恐怕无法在任何块中完成(如 given:
、setup:
、when:
)。有一个简单的解决方法。
@Grab(group='org.spockframework', module='spock-core', version='0.7-groovy-2.0')
import spock.lang.Specification
class ExampleSpec extends Specification {
def "foo"() {
def a, b
when:
a = 0
b = 1
then:
a != b
}
}
将声明移出 when:
/given:
/setup:
块并将它们作为方法变量。 setup:
/given:
标签是可选的,可以省略,从而导致隐式设置块。
虽然这行得通,但我想知道为什么这行不通。您可以创建一个 issue in github.
您应该使用 where: 块来传递参数。不需要定义,因为它们是作为参数传递的,如果您喜欢的话,您甚至可以进行多次 运行 的测试。
def "Use where to pass test data"(){
expect: 1 == myNumber
2 == myOther1
where: myNumber = 1
myOther1 = 2
}
这是 a link to some other examples I've written that show how to pass data to your tests. If you really like multiple assignment (even though it's not a good idea) here's how 您可以在 where: 块中使用它。
如果您对这里的各种块感到好奇,请参阅 a summary of everything I've read. Don't take my word for it, here's some official 文档。
您可能会很高兴知道这个问题已 raised already,但上个月 activity 还没有出现。我的假设是关于多重赋值的某些事情与 Spock 在您的代码中使用的 AST 转换不一致。
我想知道如何在不初始化的情况下在 Spock 规范中的一行中定义多个变量,如下所示。
我试过:
import spock.lang.Specification
class exampleSpec extends Specification {
def "foo"() {
when:
def a, b
a = 0
b = 1
then:
a != b
}
}
但是访问b时失败了:
No such property: b for class: ....
我已经设法使以下工作正常进行:
def (a, b) = []
但我想要更好的。
非常感谢任何帮助!
我正在使用:
Groovy Version: 2.4.3 JVM: 1.8.0_45 Vendor: Oracle Corporation OS: Linux
恐怕无法在任何块中完成(如 given:
、setup:
、when:
)。有一个简单的解决方法。
@Grab(group='org.spockframework', module='spock-core', version='0.7-groovy-2.0')
import spock.lang.Specification
class ExampleSpec extends Specification {
def "foo"() {
def a, b
when:
a = 0
b = 1
then:
a != b
}
}
将声明移出 when:
/given:
/setup:
块并将它们作为方法变量。 setup:
/given:
标签是可选的,可以省略,从而导致隐式设置块。
虽然这行得通,但我想知道为什么这行不通。您可以创建一个 issue in github.
您应该使用 where: 块来传递参数。不需要定义,因为它们是作为参数传递的,如果您喜欢的话,您甚至可以进行多次 运行 的测试。
def "Use where to pass test data"(){
expect: 1 == myNumber
2 == myOther1
where: myNumber = 1
myOther1 = 2
}
这是 a link to some other examples I've written that show how to pass data to your tests. If you really like multiple assignment (even though it's not a good idea) here's how 您可以在 where: 块中使用它。
如果您对这里的各种块感到好奇,请参阅 a summary of everything I've read. Don't take my word for it, here's some official 文档。
您可能会很高兴知道这个问题已 raised already,但上个月 activity 还没有出现。我的假设是关于多重赋值的某些事情与 Spock 在您的代码中使用的 AST 转换不一致。