views:

152

answers:

2

Is there a "best practice" or defacto standard with how much of the GORM functionality one should test in the unit/functional tests?

My take is that one should probably do most of the domain testing as functional tests so that you get the full grails environment. But what do you test? Inserts, updates, deletes? Do you test constraints even though they were probably more thoroughly tested by the grails release?

Or do you just assume that GORM does what it is supposed to do and move to other parts of the application?

+1  A: 

Personally I'd test any complex relationships which I'm not 100% comfortable with setting up, and any accessors for which the default implementation is overwritten.

Alison
That sounds reasonable, I just worry that I am testing GORM itself instead of my code. In a way of looking at things, my mappings are part of my code and I am testing that.
Lloyd Meinholz
+3  A: 

My general rule is to test what I write. Therefore, if I write custom methods (or closures) then I'll unit test those. This rule also means I'll test constraints since I've written the constraints. For that I use mockForConstraintsTests() method in GrailsUnitTestCase.

An example constraints block:

static constraints = {
      location(blank:true, nullable:true)
      make(blank:false, nullable:false)
      name(blank:false, nullable:false)
      serviceTag(nullable:true)
      purchaseDate(blank:false, nullable:false)
      checkedDate(blank:false, nullable:false)
      warrantyExpirationDate(nullable:true)
      notes(blank:true, nullable:true)
    }

I would have the following constraints unit test:

void test_null_constraints_are_checked() {
      mockForConstraintsTests(Hardware)
      def hardware = new Hardware()
      assertFalse hardware.validate()

      assertEquals 4, hardware.errors.getFieldErrorCount()
      assertEquals "nullable", hardware.errors["name"]
      assertEquals "nullable", hardware.errors["checkedDate"]
      assertEquals "nullable", hardware.errors["purchaseDate"]
      assertEquals "nullable", hardware.errors["make"]
}

This will catch any typos on my constraints right away.

I don't test saves, creates, updates, deletes at the domain; if these fail then I have bigger problems!

zentuit
Would you ever test relationships 1-M etc.?
Lloyd Meinholz
I can't say that I've tested them directly at the unit. I usually pick them up at the integration level.
zentuit