断言Spock中的项目列表

Jer*_*emy 5 spock grails-2.0

使用Spock 0.7和Grails 2.04.试图建立一个测试环境.我需要一些关于测试对象列表的帮助.

我有一个位置对象列表.我想测试每个对象的日期.我正在迭代但不确定如果日期不相等,测试会如何失败.有没有一种好方法来测试列表中的对象?我在下面列出了我的代码块.

then:
        weatherList != null
        weatherList.empty != null
        weatherList.size() == 3
        weatherList.each {
            Calendar today = Calendar.getInstance();
            today.clearTime()
            if(it.forecastDate != today) {
                return false
            }
        }
Run Code Online (Sandbox Code Playgroud)

Pet*_*ser 20

解决方案可能如下所示(内联评论):

// avoid testing with real dates if possible
def today = Calendar.getInstance().clearTime() 

when:
...

then:
weatherList != null
weatherList.size() == 3
// does this list really contain Calendar objects?
weatherList.every { it.forecastDate == today }
// OR, for a potentially better error message
weatherList.each { assert it.forecastDate == today }
Run Code Online (Sandbox Code Playgroud)