Grails和Geb:可重复的删除测试

时间:2016-03-21 15:25:53

标签: grails spock geb

我想用Geb,Grails和RemoteControl测试一个简单的CRUD删除。

这是我的简化代码:

test "Delete a book"() {
    when:
    to BookShowPage // book/show/1
    deleteLink.click(BookListPage)

    then:
    // ...

    cleanup:
    def remote = new RemoteControl()
    remote {
        new Book(title:'title').save(failOnError: true, flush: true)
        return true
    }
}

但是如何让我的测试重现?

如果我重复我的测试,新书将有另一个ID,因此测试失败。

1 个答案:

答案 0 :(得分:1)

根据railsdog的评论,我决定在给定的区块中创建一本新书。

这里是正确的代码:

test "Delete a book"() {
    given:
    def remote = new RemoteControl()
    def bookId = remote {
        Book book = new Book(title:'title').save(failOnError: true)
        return book.id
    }

    when:
    to BookShowPage, bookId // (*)
    deleteLink.click(BookListPage)

    then:
    // ...

    cleanup:
    remote {
        Book book = Book.get(bookId)
        if (book) { 
            // Test fails and we have junk into the db...
            book.delete(failOnError: true) 
        }
        return true
    }
}

(*)见how to customize Geb page URL