简体   繁体   English

Grails Spock单元测试不断失败?

[英]Grails Spock unit test keeps failing?

I am currently trying to Unit Test a domain model's constraints in Grails using the Spock framework and I am having some issues. 我目前正在尝试使用Spock框架对Grails中的域模型约束进行单元测试,但遇到了一些问题。 So I have the following domain model with the various constraints: 因此,我具有以下受各种约束的领域模型:

class profile {

    String phoneNo
    String userName

    static belongsTo = [group:Group]

    static constraints = {
        phoneNo(blank:false, maxsize:14, matches:"44[0-9]{10}")
    }

}

Then I have this test that should be able to test each of the field constraints one at a time and return the expected results: 然后,我进行了此测试,该测试应能够一次测试每个字段约束并返回预期结果:

@Unroll("test profile all constraints #field is #error")
    def "test profile all constraints"() {
        when:
        def newObj = new Profile("$field": val)

        then:
        validateConstraints(newObj, field, error)

        where:
        error                  | field        | val
        'blank'                | 'phoneNo'    | '447897654321'
        'maxSize'              | 'phoneNo'    | '123456789012'
        'matches'              | 'phoneNo'    | getPhoneNumber(true)

    }

However when I run the test say for the Phone Number Field's Max Size constraint and pass it a value smaller than the max size available I would expect this test to pass but it fails, in fact all of the tests fail and I am unsure why as I am new to using this framework. 但是,当我运行测试时,请说出“电话号码字段”的“最大大小”约束,并将其传递的值小于可用的最大大小,我希望该测试能够通过,但失败了,实际上所有测试都失败了,我不确定为什么我是使用此框架的新手。 I would really appreciate some help on this. 我真的很感谢这方面的帮助。

Thanks in advance 提前致谢

I have now managed to fix this issue. 我现在设法解决了这个问题。

The issue was related to the mocking of the constraints, I was mocking up the wrong constraints for the test I wanted to do. 问题与约束的模拟有关,我在为要执行的测试模拟错误的约束。

Thanks for the help 谢谢您的帮助

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

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