尝试使用beforeDelete事件对对象进行集成测试Grails GORM删除。问题是在Integration Tests中没有触发beforeDelete事件:
class ChallengeMembershipTests extends GroovyTestCase {
void testQuitChallenge() {
VUser user = VUser.build()
assertNotNull("user should be saved", user.id)
UserChallenge challenge = UserChallenge.build(owner: user)
assertNotNull("challenge should be saved", challenge.id)
ChallengeMember member = ChallengeMember.build(challenge: challenge)
println "MEMBER: ${member}"
assertNotNull("member should be saved", member.id)
member.delete(flush:true)
}
}
ChallengeMember的相关部分:
def beforeDelete = {
log.info "begin deleting challenge member ${this}"
user.removeFromChallenge_memberships(this)
challenge?.removeFromParticipants(this)
team?.removeFromMembers(this)
log.info "deleting challenge member ${this}"
}
Grails 1.3.5
有什么建议吗?
为什么我在乎?
由于以下原因,我的测试失败了:
org.springframework.dao.InvalidDataAccessApiUsageException:已删除的对象将通过级联重新保存(从关联中删除已删除的对象):[com.v.ChallengeMember#1];嵌套异常是org.hibernate.ObjectDeletedException:已删除的对象将通过级联重新保存(从关联中删除已删除的对象):[com.v.ChallengeMember#1]
但是,删除在浏览器中工作正常,因为beforeDelete正在被触发。
答案 0 :(得分:1)
好吧,如果我在withNewSession中输入它会触发 -
ChallengeMember.withNewSession {
membership.delete(flush:true)
}