Lloyd Meinholz
Lloyd Meinholz

Reputation: 2600

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?

Upvotes: 7

Views: 620

Answers (2)

zentuit
zentuit

Reputation: 716

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!

Upvotes: 5

Armand
Armand

Reputation: 24343

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.

Upvotes: 1

Related Questions