【问题标题】:Grails and Geb: Reproducible deletion testGrails 和 Geb:可重现的删除测试
【发布时间】:2016-03-21 15:25:52
【问题描述】:

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

【问题讨论】:

  • 在给定的块中设置您的书:块并检索书的 ID,使“到 BookShowPage”使用在给定块中添加的书的 ID(我假设你可以这样做这个,我自己还没有尝试过 Geb),你应​​该可以复制吗?
  • 这是个好办法。我只需要发现 hot 就可以将 id 传递给 geb 页面...谢谢。

标签: grails spock 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多