GORM事件未在集成测试中触发

时间:2010-12-08 10:47:51

标签: grails gorm grails-domain-class

尝试使用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正在被触发。

1 个答案:

答案 0 :(得分:1)

好吧,如果我在withNewSession中输入它会触发 -

ChallengeMember.withNewSession {
   membership.delete(flush:true)
}