Skip to content

Commit 10c0233

Browse files
authored
Full GrailsUnitSpec example
For reference.
1 parent 8c06ac8 commit 10c0233

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,38 @@ When running a unit test, the cascade constraint isn't registered with Grails. T
5454
`org.grails.testing.GrailsUnitTest` and the following code must be added to the `setup()` method of the test:
5555

5656
```groovy
57-
@Override
58-
Closure doWithSpring() {
59-
return {
60-
constraintEvaluator(DefaultConstraintEvaluator)
61-
}
62-
}
57+
import com.cscinfo.platform.constraint.CascadeConstraintRegistration
58+
import org.grails.datastore.gorm.validation.constraints.eval.DefaultConstraintEvaluator
59+
import org.grails.testing.GrailsUnitTest
60+
import spock.lang.Specification
61+
62+
class ParentSpec extends Specification implements GrailsUnitTest {
63+
64+
@Override
65+
Closure doWithSpring() {
66+
return {
67+
constraintEvaluator(DefaultConstraintEvaluator)
68+
}
69+
}
70+
71+
void setup() {
72+
CascadeConstraintRegistration.register(applicationContext)
73+
}
6374
64-
def setup() {
65-
CascadeConstraintRegistration.register(applicationContext)
75+
void 'validate cascade'() {
76+
given:
77+
def phone = new PhoneNumber(telephoneType: new PhoneNumber.TelephoneType())
78+
79+
when:
80+
parent.validate(['telephoneType'])
81+
82+
then:
83+
parent.hasErrors()
84+
85+
parent.errors.getFieldError('telephoneType.id').code == 'nullable'
86+
parent.errors.getFieldError('telephoneType.countryCodeRecommended').code == 'nullable'
87+
}
6688
}
89+
6790
```
6891
This will register the `CascadeConstraint` the same way as the plugin does at runtime.

0 commit comments

Comments
 (0)