我在 groovy
中编写了以下测试(同时使用 spock
框架):
def "#checkPassword check if passwd match"() {
given:
def allowedPasswords = ["1", "2"]
expect:
myStrategy.checkPassword(myModel, input) == result
where:
input | result
allowedPasswords | true
}
但是,当我运行它时,allowedPasswords
字段似乎丢失了。我收到以下错误:
groovy.lang.MissingPropertyException: No such property: allowedPasswords for class:
我想知道为什么,因为我在 given
部分声明了它。你能帮我吗?
最佳答案
看起来你在寻找@Shared
:
import spock.lang.Shared
import spock.lang.Specification
class SpockTest extends Specification {
@Shared allowedPasswords = ["1", "2"]
def "#checkPassword check if passwd match"() {
expect:
checkPassword(input) == result
where:
input << allowedPasswords
result << allowedPasswords
}
static String checkPassword(String input) {
return input
}
}
关于unit-testing - 在 spock 中运行测试时出现 MissingPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51600906/