声明Spock中的项目列表 [英] Asserting on a list of items in Spock

查看:75
本文介绍了声明Spock中的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Spock 0.7与Grails 2.04一起使用.试图建立一个测试环境.在测试对象列表方面,我需要一些帮助.

Using Spock 0.7 with Grails 2.04. Trying to set up a testing environment. I need some help in regards to testing a list of objects.

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

I have a list of location objects. I want to test a date on each of those objects. I am iterating over but not sure how to make the test fail if the dates are not equal. Is there a good way to test objects in a list? I have listed below my then block of code.

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

推荐答案

解决方案可能如下所示(内联注释):

A solution could look like this (comments inlined):

// 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 }

这篇关于声明Spock中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆