我有以下用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
    }
}

09-26 15:41