开发者

How much of Grails GORM to test?

开发者 https://www.devze.com 2022-12-29 09:20 出处:网络
Is there a \"best practice\" or defacto standard with how much of the GORM functionality one should test in the unit/functional tests?

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 开发者_JS百科and move to other parts of the application?


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!


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.

0

精彩评论

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

关注公众号