将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
}
}
答案 0 :(得分: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 }