List Groovy:从字符串列表中弹出最后一个元素
在我的spock测试课程中,我有以下两个列表:List Groovy:从字符串列表中弹出最后一个元素,list,testing,groovy,spock,geb,List,Testing,Groovy,Spock,Geb,在我的spock测试课程中,我有以下两个列表: @Shared def orig_list = ['東京(成田・羽田)', '日本','アジア' ] @Shared def dest_list = ['ソウル', '韓国','アジア' ] def "Select origin"() { when: something() then: do_something() where: area << orig_list.pop
@Shared def orig_list = ['東京(成田・羽田)', '日本','アジア' ]
@Shared def dest_list = ['ソウル', '韓国','アジア' ]
def "Select origin"()
{
when:
something()
then:
do_something()
where:
area << orig_list.pop()
country << orig_list.pop()
port << orig_list.pop()
dest_area << dest_list.pop()
dest_country << dest_list.pop()
dest_port << dest_list.pop()
}
但如果我不使用where block和do like:
def "Select origin"()
{
def area = orig_list.pop()
def country = orig_list.pop()
def port = orig_list.pop()
def dest_area = dest_list.pop()
def dest_country = dest_list.pop()
def dest_port = dest_list.pop()
when:
something()
then:
do_something()
}
比它好用
如何从列表中获取where块中的值?问题出在哪里?where块中定义的变量需要列表,但是pop()方法返回列表中的一个元素,在本例中,该元素似乎是字符串 将
list.pop()
用括号括起来,就像这样[list.pop()]
或者,更好的做法是,重写where块以使用列语法,例如:
where:
area | country | port | dest_area | dest_country | dest_port
'a1' | 'c1' | 'p1' | 'da1' | 'dc1' | 'dp1'
'a2' | 'c2' | 'p2' | 'da2' | 'dc2' | 'dp2'
where块中定义的变量需要列表,但pop()方法返回列表中的一个元素,在本例中,该元素似乎是字符串 将
list.pop()
用括号括起来,就像这样[list.pop()]
或者,更好的做法是,重写where块以使用列语法,例如:
where:
area | country | port | dest_area | dest_country | dest_port
'a1' | 'c1' | 'p1' | 'da1' | 'dc1' | 'dp1'
'a2' | 'c2' | 'p2' | 'da2' | 'dc2' | 'dp2'
非常感谢你的回答。我没有注意到block在哪里需要一个列表。非常感谢你的帮助回答。我并没有注意到where block在那里期待一个列表。