Wednesday, 15 August 2012

grails - unit testing the unique constraint on domain class -



grails - unit testing the unique constraint on domain class -

i'm using grails 2.4.1 , having problem understanding how test unique constraint on domain class.

my domain class looks like: class person {

string name static constraints = { name( unique: true, blank: false ) } }

and test:

@testfor(person) @testmixin(grailsunittestmixin) class personspec extends specification { void "name should unique"() { given: mockforconstraintstests person when: def 1 = new person( name: 'john' ) then: one.save() when: def 2 = new person( name: 'john' ) then: ! two.validate() two.errors.hasfielderrors( 'name' ) }

the sec instance validating despite apparently violating unique constraint. can please explain what's going on here , how should best right it?

thanks!

--john

i not think best approach test constraints triggering them. generally want test our code working , not grails validating constraints.

why test framework (i.e. grails)? grails people have done :-) (yes, there situations makes sense check framework, don't think one).

so thing test have placed right constraints on domain class:

@testfor(person) class personspec extends specification { void "name should have unique/blank constraints"() { expect: person.constraints.name.getappliedconstraint('unique').parameter ! person.constraints.name.getappliedconstraint('blank').blank } }

if worth writing test question...

unit-testing grails

No comments:

Post a Comment