【问题标题】:Why doesn't a spaces only value for a field that has the blank: false constraint fail validation为什么只有空格的字段没有空格值:false 约束验证失败
【发布时间】:2018-03-16 00:55:00
【问题描述】:

我在域类中有一个字段,我对其应用了空白:假约束,我编写了一个单元测试来验证该字段的仅空格字符串未通过验证,但测试失败:

void 'test name cannot be blank'() {

    when:
    domain.name = ' '

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

我这是ConditionNotSatisfiedError线上的错误!domain.validate(['name']): ConditionNotSatisfiedError

这是我的域对象:

class Thing {

    String name

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

【问题讨论】:

  • 你能展示你对域对象的约束吗?

标签: unit-testing grails


【解决方案1】:

您没有说明您使用的是什么版本的 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'
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多