Search code examples
grailsspockgeb

Grails and Geb: Reproducible deletion test


I want to test a simple CRUD deletion with Geb, Grails and RemoteControl.

Here my simplified code:

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

But how can I make my test reproducible?

If I repeat my test, the new book will have another id and so test fails.


Solution

  • According to railsdog's comment, I resolved creating a new Book in the given block.

    Here the right code:

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

    (*) See how to customize Geb page URL.