I am trying to write some unit tests to test Command objects. When my command object has many fields with many validation rules, setting the command object for each test case becomes too verbose and repetitive.
Let's say I have this command object:
class MemberCommand {
String title
String name
String phone
static constraints = {
title(blank: false, inList: ["Mr", "Mrs", "Miss", "Ms"])
name(blank: false, maxSize:25)
phone(blank: false, matches: /\d{8}/)
}
}
I want to verify this by doing something like this:
class ValidationTitle extends UnitSpec {
def "title must be one of Mr, Mrs, Miss, Ms"() {
setup:
def memberCommand = new MemberCommand()
mockForConstraintsTests MemberCommand, [memberCommand]
when:
memberCommand.title = t
then:
memberCommand.validate() == result
where:
t << ["Mr", "Mrs", "Miss", "Ms", "Dr", ""]
result << [true, true, true, true, false, false]
}
}
, , memberCommand.validate(), , "Mr" .
, , , . , , .
( Spock) grails?
, ?
.