我有一个应该创建一个域对象的方法。但是,如果在构建对象的过程中出现条件,则该方法应该返回并且不保存该对象。如何创建Grails域对象但不保存?
考虑:
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,即为什么可以找到我的对象?我认为它只会找到保存到数据库的对象。我应该测试什么来查看我的对象是否未创建?
你是完全正确的,谢谢。这个错误是因为我在一个EachWithIndex闭包中进行了更改,然后调用了返回值,认为闭包后的保存调用不会被调用。我现在意识到,闭包内的返回调用不会从包含闭包的方法返回,它只从闭包本身返回。 – John
@Burt谢谢,救了我一天。我在搜索一个Domain类命令对象如何在脏时被持久化,在grails文档命令对象部分进行搜索,但在这里更加清楚。我认为这应该在那里,或者我错过了一些东西。 –