【问题标题】:How can I check persisted data inside an integration test如何检查集成测试中的持久数据
【发布时间】:2012-07-05 06:11:11
【问题描述】:

在我的集成测试中,我执行以下操作:

void testSave() {
    def controller = new BookController()
    controller.params.title = "Awesome Book"
    controller.params.pages = 1000
    controller.save()
    // i want to check book data here
}

如何检索我的控制器持续存在的Book 并检查title == "Awesome Book"pages == 1000 是否存在?我可以通过测试以某种方式访问​​控制器中的bookInstance 变量吗?

我不能假设Book.get(1) 会给我正确的Book,因为数据库中会有引导数据。控制器重定向到完全不相关的 Book 页面,因此我无法从 URL 中获取 id。我唯一能想到的就是做这样的事情:

void testSave() {
    def oldIdList = Book.list()*.id
    // set up the parameters and call controller.save()
    def insertedId = Book.list()*.id - oldIdList
    // check Book.get(insertedId) properties
}

但我希望有更好的方法......

【问题讨论】:

    标签: testing grails integration-testing


    【解决方案1】:

    假设您使用的是 Grails 2.0.x,您可以使用 findBy* 方法通过它的任何属性来查找该书:

    def book = Book.findByTitle("Awesome Book")
    assertNotNull book
    

    【讨论】:

    • 动态查找器不是特定于 2.0 的
    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2015-02-13
    • 2018-03-10
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多