我想用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,因此测试失败。
答案 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
}
}