为什么具有空白的字段的空格值不是唯一的值:false 约束验证失败

Why doesn't a spaces only value for a field that has the blank: false constraint fail validation

我在域 class 中有一个字段,我应用了 blank: false 约束,我编写了一个单元测试来验证该字段的纯空格字符串没有通过验证,但是测试失败:

void 'test name cannot be blank'() {

    when:
    domain.name = ' '

    then:
    !domain.validate(['name'])
    domain.errors['name'].code == 'blank'
}

我在 !domain.validate(['name']) 行出现 ConditionNotSatisfiedError 错误:ConditionNotSatisfiedError

这是我的域对象:

class Thing {

    String name

    static constraints = {
        name nullable: false, blank: false, size: 1..20
    }
}

您没有说明您使用的是哪个版本的 Grails,这可能是相关的。 https://github.com/jeffbrown/selenablank 的项目演示了它在 3.3.3 中的工作原理。

https://github.com/jeffbrown/selenablank/blob/master/grails-app/domain/selenablank/Thing.groovy

package selenablank

class Thing {
    String name

    static constraints = {
        name nullable: false, blank: false, size: 1..20
    }
}

https://github.com/jeffbrown/selenablank/blob/master/src/test/groovy/selenablank/ThingSpec.groovy

package selenablank

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class ThingSpec extends Specification implements DomainUnitTest<Thing> {

    void 'test name cannot be blank'() {

        when:
        domain.name = ' '

        then:
        !domain.validate(['name'])
        domain.errors['name'].code == 'blank'
    }
}