如何在 Groovy test with spoc 中测试嵌套列表对象的属性

How to test properties of a nested list object in Groovy test with spoc

我正在编写 groovy 测试。我的响应对象应该是这样的:

[ school: new School( 
id: "School1", 
name: "School1", 
courses: [ 
new Course(id: "Course1", name: "Course1"),
new Course(id: "Course2", name: "Course2")])]

下面是我的测试:

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

}

我想不出如何将测试中的 'and' 部分写成嵌套集合。

我觉得这个可以断言:

result.every { it.courses.every { course => course.id == course.name }

在每所学校中,每门课程名称必须等于其课程 ID。

我宁愿在此处使用 Groovy 电源断言,以便在不匹配的情况下获得描述性错误消息:

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
}

*.the Groovy spread operator

但是,如果您想知道哪个学校有不匹配的课程,那么 and: 部分会更详细一点