2016-03-21 41 views
1

我想用Geb,Grails和RemoteControl測試一個簡單的CRUD刪除。Grails和Geb:可重複刪除測試

這裏我的簡化代碼:

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

在給定的塊中執行您的書籍設置並檢索書的ID,使'BookShowPage'消耗給定塊中添加的書的ID(我假設您可以做到這一點,沒有冒險到Geb但我自己),你應該可複製? – railsdog

+0

這是一個好方法。我只有發現熱點將id傳遞給geb頁面... 謝謝。 –

回答

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