I am writing a groovy test. My response object should like this:
[ school: new School(
id: "School1",
name: "School1",
courses: [
new Course(id: "Course1", name: "Course1"),
new Course(id: "Course2", name: "Course2")])]
Below is my test:
def "should update the name of the school and course as per their id"() {
given:
def request = requestObject
when:
def response = myService.update(requestObject)
then: "name of the school should be equal to its id"
result.collect {
it.name == it.id
}.every { it }
and: "name of each course should be equal to its id"
//Need help
}
I can't think of how to write the 'and' part in my test as its a nested collection.
I'd rather use Groovy power assert here in order to get descriptive error messages in case of mismatches:
then: "name of the school should be equal to its id"
result.each {
assert it.name == it.id
}
and: "name of each course should be equal to its id"
result*.courses*.each {
assert it.name == it.id
}
*.
is the Groovy spread operator
However, if you want to know which school has mismatched courses then it will be a bit more verbose in the and:
section