Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much of Grails GORM to test?

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?

like image 467
Lloyd Meinholz Avatar asked May 05 '10 04:05

Lloyd Meinholz


1 Answers

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!

like image 113
zentuit Avatar answered Oct 08 '22 03:10

zentuit