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:
I am looking to do this in a Unit test - is this right/appropriate?
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.
精彩评论