我正在测试使用Geb/Spock在我的网站上创建然后删除记录。然而,我不能创建记录,如果它已经存在,所以我检查记录的存在,并删除它,如果它存在于测试的开始。当记录不存在时出现问题,导致测试失败。是否有一种方法来合并一些if/then/else逻辑,以便测试将继续,如果它没有在开始找到记录,并删除它,如果它找到它?
编辑示例代码:/**
* Integration test for Create Record
**/
class CreateAndRemoveRecordSpec extends GebSpec {
def 'check to make sure record 999 does not exist'() {
given: 'user is at Account Page'
to MyAccountPage
when: 'the user clicks the sign in link'
waitFor { header.signInLink.click() }
and: 'user logs on with credentials'
at LoginPage
loginWith(TEST_USER)
then: 'user is at landing page.'
at MyAccountPage
and: 'list of saved records is displayed'
myList.displayed
/* I would like some sort of if here so the test doesn't fail if there is no record*/
when: 'record 999 exists'
record(999).displayed
then: 'remove record 999'
deleteRecord(999).click()
/* continue on with other tests without failing whether or not the record exists */
}
def 'test to create record 999'() {}
def 'test to remove record 999'() {}
你可以这样做:
when: 'record 999 exists'
def displayed = record(999).displayed
then: 'remove record 999'
!displayed || deleteRecord(999).click()
如果记录(999)没有显示,那么!displayed
语句将被计算为true,因此deleteRecord(999).click()
不应该被计算,因此导致测试通过。
当显示记录时,!displayed
将计算为false,因此spock必须计算deleteRecord(999).click()
语句,以提供所需的行为。
这是基于短路求值(Java和Groovy都使用)http://en.wikipedia.org/wiki/Short-circuit_evaluation