unit-testing - 在 spock 中运行测试时出现 MissingPropertyException

标签 unit-testing groovy spock

我在 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/

相关文章:

ios - 如何使用异步调用对 @IBAction 进行单元测试

soap - 在 groovy 中使用的标准 SOAP 库是什么?

regex - 使用正则表达式和 OR 获取组

unit-testing - Grails Spock单元测试不一致

java - 如何测试 util detector OS 类

java - 使用 main() 方法运行数据库适配器测试

html - Polymer 2.x 单元测试将可选内容传递给 fixture 以绑定(bind)到目标属性

java - 如何使用http post 连接grails 登录系统?

spring-boot - 使用 Spring Boot 和 Spock 进行模拟服务

testing - 使 Grails Geb 测试具有事务性