考虑以下代码:
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 > email 应该是唯一的 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
这正是我正在做的,我在这里错过了什么?