开发者

Grails - testing GORM relationships

Bit of a newbie to Grails here. I have 2 domain objects linked with a hasMany relationship like so:

    class Accommodation
    {
      String id
      String n开发者_如何学Goame

      static hasMany = [ accommodationDescription : AccommodationDescription ]

    }

    class AccommodationDescription
    {
      // Accommodation
      Accommodation accommodation

      // Description
      Description description

      static belongsTo = [accommodation : Accommodation]
   }

I have written some code which tests the cascade delete functionality between them. My question is two-fold:

  1. I am looking to do this in a Unit test - is this right/appropriate?

  2. I have a testXXX(..) method that attempts the cascade delete like so:

    void testAccDescDelete()

    {

    Accommodation acc = ...create a populated instance
    
    assert acc.save() // this passes!
    
    acc.delete() // no errors here  
    
    assert  ! acc.hasErrors()   // this passes!
    
    assert  acc.accommodationDescription == null // this fails.
    

    }

But this does not work and the assertion fails. Could someone please tell me why?


I don't believe calling delete on the object sets all the fields to null. It deletes the data from the database and the instance becomes detached from the hibernate session. The data in your containing object remains in memory as a transient instance.

To verify that the object has been deleted in your test you can try to get() it by ID or use a findBy() method. Then assert that nothing was found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜