grails - 当我希望在许多相似的领域执行相同的测试时,如何遍历Grails Spock测试中的测试?

标签 grails integration-testing spock

我有一个带有属性对的域类:

Boolean statement1
Boolean statement1Missing

这些对最多为九对。

以下约束适用于每个配对:
    statement1                  nullable: true
    statement1Missing           validator: {val, obj ->
                                    if ( val == true &&
                                        (obj.statement1 == true || obj.statement1 == false)) {
                                        return ['statement.not.missing', 1] 
                                    }
                                    if (obj.statement1 == null && val == false) {
                                        return ['statement.is.missing', 1]
                                    }
                                }

我设计了一个可以容纳一对语句的集成测试:
@Unroll
def "Validation of custom validation for statement1"() {

    given: "New data"
    def participant = new Data(studyId:     "M00001",
                                            formVersion:    Version.get(7)                          
                                           )

    and: "an initial set of test values"
    participant.statement1 = statement1
    participant.statement1Missing = statement1Missing

    when: "the validator is invoked"
    def isValidConsentData = participant.validate()

    then: "the appropriate fields are flagged as errors"
    isValidConsentData == anticipatedValid
    participant.errors.getFieldError(fieldInError)?.code == errorCode

    where:

    statement1  | statement1Missing | anticipatedValid  | fieldInError          | errorCode
    true        | false             | true              | null                  | null
    false       | false             | true              | null                  | null
    null        | true              | true              | null                  | null
    null        | false             | false             | "statement1Missing"   | "statement.is.missing"
    true        | true              | false             | "statement1Missing"   | "statement.not.missing"
    false       | true              | false             | "statement1Missing"   | "statement.not.missing"
}

这处理了我要测试的statement1的所有组合。

我一直在尝试找出如何对所有九个语句对重复此测试。我尝试过像这样的循环:
    (1..9).each { statementNo
        ...
        and ...
        participant.("statement" + statementNo) = ("statement" + statementNo)
        ...
        where:
        ("statement" + StatementNo) | ("statement" + StatementNo + Missing) | ...
    }

在想要遍历属性之前,我曾使用过这种类型的迭代,但是在Spock中不起作用。只是完全忽略了该测试。我真的不想为每个语句对重复此代码。

我研究了这种类型的结构http://www.christianoestreich.com/2012/11/domain-constraints-grails-spock-updated/的用法,但这只允许您一次测试一个属性值,而我想多次测试一个属性值。

另一个选择是在“where”块中显式包括每个属性对以及每个可能的结果,但这将非常麻烦。

请提供一些有关如何使用迭代结构执行这些测试的建议。

最佳答案

怎么样:

...    

and: "an initial set of test values"
(1..9).each { statementNo ->
    participant."statement$statementNo" = statement
    participant."statement${statementNo}Missing" = statementMissing
}

when: "the validator is invoked"
def isValidConsentData = participant.validate()

then: "the appropriate fields are flagged as errors"
(1..9).each { statementNo ->
    def fieldInError
    if (anticipatedValid) {
        fieldInError = null
    } else {
        fieldInError = "statement${statementNo}Missing"
    }
    assert isValidConsentData == anticipatedValid
    assert participant.errors.getFieldError(fieldInError)?.code == errorCode
}

where:
statement  | statementMissing | anticipatedValid | errorCode
true       | false            | true             | null
false      | false            | true             | null
null       | true             | true             | null
null       | false            | false            | "statement.is.missing"
true       | true             | false            | "statement.not.missing"
false      | true             | false            | "statement.not.missing"

如果给定字段没有错误,则不确定getFieldError(fieldName)的行为,因此,如果抛出异常,则可能需要使用!anticipatedValid为第二个断言添加一些条件。

重要的是隐式assert是必需的,因为each无效,因此测试根本不会进行任何检查。

关于grails - 当我希望在许多相似的领域执行相同的测试时,如何遍历Grails Spock测试中的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183758/

相关文章:

grails - 删除即使在服务中也不起作用?

java - 如何使用自定义协议(protocol)测试网络应用程序?

unit-testing - 使用 Jersey 测试框架输出 HTTP 响应?

grails - 运行集成测试时不存在Grails动态方法

spring - 运行 Spring 测试 : JdbcSQLSyntaxErrorException: Column "start_value" not found 时如何修复 H2 插件(版本 1.4.200)的错误

unit-testing - 为什么将空字符串转换为 null 传递给 Grails 2.4.0 中域对象的构造函数?

maven - Maven生命周期错误阶段发生的Grails单元测试

grails - 监视Netbeans中的Java堆空间

grails - 在Grails中,通过g:set将taglib返回值传递给 View

unit-testing - 如何对 Go Gin 处理程序函数进行单元测试?