【发布时间】:2015-09-12 00:22:17
【问题描述】:
考虑以下代码:
class User {
static constraints = {
email email: true, unique: true
token nullable: true
}
String email
String password
String token
}
@TestFor(User)
@TestMixin(DomainClassUnitTestMixin)
class UserSpec extends Specification {
def "email should be unique"() {
when: "twice same email"
def user = new User(email: "test@test.com", password: "test")
def user2 = new User(email: "test@test.com", password: "test")
then: "saving should fail"
user.save()
!user2.save(failOnError: false)
}
}
使用以下配置(部分),application.yml:
grails:
gorm:
failOnError: true
autoFlush: true
user2.save(failOnError: false) 没有保存到数据库中,为什么没有返回 false?
运行输出:grails test-app *UserSpec:
businesssoftware.UserSpec > 电子邮件应该是唯一的 FAILED org.spockframework.runtime.ConditionNotSatisfiedError at UserSpec.groovy:40
当我将 user.save() 替换为 user.save(flush: true) 时,它确实起作用了。
但是https://grails.github.io/grails-doc/latest/guide/conf.html 的文档第 4.1.3 节声称:
grails.gorm.autoFlush - 如果设置为 true,则导致合并、保存和删除方法刷新会话,而不需要使用 save(flush: true) 显式刷新。
作为参考,这是grails --version的输出:
| Grails 版本:3.0.2
| Groovy 版本:2.4.3
| JVM版本:1.8.0_40
这正是我正在做的,我在这里错过了什么?
【问题讨论】:
标签: grails grails-orm grails-3.0