简体   繁体   English

在Grails 3单元测试中使用Spock数据驱动的测试

[英]Using Spock data driven tests in Grails 3 unit test

I am upgrading a series of unit tests that worked in Grails 2 to Grails 3 and having problems with domain tests that use the Spock data driven test format to drive tests. 我正在将一系列在Grails 2中可以使用的单元测试升级到Grails 3,并且使用Spock数据驱动的测试格式来驱动测试的域测试遇到了问题。

import grails.test.mixin.TestFor
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.Unroll

import com.spiekerpoint.ark.test.ConstraintsTestSupport

 /**
  * Unit test for domain class ContactPhoneNumber.
  */
 @TestFor(ContactPhoneNumber)
 class ContactPhoneNumberSpec implements ConstraintsTestSupport {

    def setup() {
    }

    def cleanup() {
    }

    @Unroll
    void "test ContactPhoneNumber type constraints"() {
        when:
        ContactPhoneNumber obj = new ContactPhoneNumber("$field": val)

        then:
        validateConstraints(obj, field, error)

        where:
        error       | field         | val
       'valid'      | 'type'        | 'HOME'
       'valid'      | 'type'        | 'MOBILE'
       'valid'      | 'type'        | 'WORK'
       'valid'      | 'type'        | 'PAGER'
       'valid'      | 'type'        | 'FAX'
    }
}

I am getting a groovy.lang.MissingPropertyException at ContactPhoneNumberSpec.groovy:26 我在ContactPhoneNumberSpec.groovy:26处收到了groovy.lang.MissingPropertyException

when I execute the test because the test execution is not filling in the error, field and val references in the test. 当我执行测试时,因为测试执行未填写测试中的错误,字段和val引用。 As mentioned, this test works with Grails 2.5.5, so I suspect that I'm missing something that I need in Grails 3. 如前所述,该测试适用于Grails 2.5.5,因此我怀疑我缺少了Grails 3中需要的东西。

Edited: I removed the mockForConstraints() call that I originally had in setup() to no effect. 编辑:我删除了我最初在setup()中具有的mockForConstraints()调用,但没有任何效果。

It turns out that the ConstraintsTestSupport trait used was implemented as a class in the old Grails 2.x project and that class extended spock.lang.Specification. 事实证明,使用的ConstraintsTestSupport特性是在旧的Grails 2.x项目中作为类实现的,并且该类扩展了spock.lang.Specification。 When it was converted to a trait it no longer extended the base Spock class needed to support data driven testing. 将其转换为特征后,它不再扩展支持数据驱动测试所需的基本Spock类。

The bug was introduced in a completely separate module and it was obvious enough if you look at the code just as a Spock test. 该错误是在一个完全独立的模块中引入的,如果您将代码视为Spock测试,就很明显了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM