Вопрос

I am currently trying to Unit Test a domain model's constraints in Grails using the Spock framework and I am having some issues. 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 с атрибуция
Не связан с StackOverflow
scroll top