【发布时间】:2014-12-19 17:03:41
【问题描述】:
我有一个应该创建域对象的方法。但是,如果在构建对象的过程中发生了某种情况,则该方法应该只返回而不保存对象。
给定:
class SomeDomainObjectClass {
String name
}
class DomCreatorService {
def createDom() {
SomeDomainObjectClass obj = new SomeDomainObjectClass(name: "name")
// do some processing here and realise we don't want to save the object to the database
if (!dontWannaSave) {
obj.save(flush: true)
}
}
}
在我的测试中(服务是 DomCreatorService 的实例):
expect: "it doesn't exist at first"
SomeDomainObjectClass.findByName("name") == null
when: "we attempt to create the object under conditions that mean it shouldn't be saved"
// assume that my test conditions will mean dontWannaSave == true and we shouldn't save
service.createDom()
then: "we shouldn't be able to find it and it shouldn't exist in the database"
// check that we still haven't created an instance
SomeDomainObjectClass.findByName("name") == null
我的最后一行失败了。为什么最后一个 findByName 返回 true,即为什么我的对象可以找到?我认为它只会找到保存到数据库中的对象。我应该测试什么以查看我的对象是否尚未创建?
【问题讨论】:
标签: hibernate grails grails-orm