如何使用spoc在Groovy测试中测试嵌套列表对象的属性
我正在写一个groovy测试。我的响应对象应该是这样的:如何使用spoc在Groovy测试中测试嵌套列表对象的属性,groovy,collections,spock,Groovy,Collections,Spock,我正在写一个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
[ 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 power assert,以便在不匹配的情况下获得描述性错误消息:
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
}
*。
是
但是,如果您想知道哪所学校的课程不匹配,那么在和:
部分将更加详细