Testing for the Default Value

828 Views Asked by At

I am just starting to write tests for a Grails app and I have watched this and read this and would like to use the tests to check that the constraints have been set properly.

Now, while at it, I would figure one should also test if the default value is correct (kind of a safetycheck if someone was tampering with the domain object). Problem seems to be that if I mock an object, it will exist, but the default value will not have been set. Is there a way to check this, or am I just being to cautious (spell 'paranoid')?

It should become something like this:

class Event implements Taggable {

   ...
   Boolean publish
   ...

   static constraints = {
        ...
        publish nullable: true, defaultValue: true
        ...
    }
}

and

@TestMixin(GrailsUnitTestMixin)
@TestFor(Event)
class EventSpec extends Specification{

    Event ev = new Event(start: new Date(), subProgram: new Subprogram(),venue: new Venue())

    def setup() {
        mockForConstraintsTests(Event)
    }

    def cleanup() {
    }

    void "publish ist default true"() {  // cannot be tested ???

        expect:
        ev.publish
    }

Is this because the DB is not involved (no environment availbale in unit tests) which sets the default value? Can I do this only in integration tests?

1

There are 1 best solutions below

0
On BEST ANSWER

You are correct, you will need to do this in an integration test, since not all the database components are wired up in a unit test.