【问题标题】:executeUpdate() not updating on grails spock-integration testingexecuteUpdate() 未在 grails spock-integration 测试中更新
【发布时间】:2015-10-30 06:15:38
【问题描述】:

嗨,我是 grails 测试的新手。愿意进行如下集成测试,但是 问题是executeUpdate() 似乎没有更新值

如何进行集成测试 executeUpdate('update query goes here')??

请帮忙推荐一下 给出了问题演示的示例代码。 提前致谢。

def "for given merchantTier Id update merchantTier value"(){
    setup:
    def merchantTier = new MerchantTier(              
            value:1.11).save(flush: true) //it saves merchantTier

    when:"when update with setProperty"
    testData = editWithSetProperty(merchantTier.id) //id is passed

    then:"it updates data and test is success"
    merchantTier.value == 2.22

    when:"executeUpdate query is used instead"
    testData = editWithExecuteUpdate(merchantTier.id)// id is passed

    then:"it does not update the data and test is failed"
    merchantTier.value == 3.33
}

def editWithSetProperty(id) {
    def merchantTier = MerchantTier.get(id.toLong())
    merchantTier.setValue(2.22.toDouble())
}

def editWithExecuteUpdate(id) {
        MerchantTier.executeUpdate('update MerchantTier mt set mt.value=:mrValue where mt.id=:mtId', [mrValue: 3.33.toDouble(), mtId: id.toLong()])
}

如何进行集成测试 executeUpdate('update query goes here')??

【问题讨论】:

标签: grails groovy integration-testing spock


【解决方案1】:

当您通过 executeUpdate 进行更新时,您再次需要从数据库中获取对象,因此请尝试在 editWithExecuteUpdate 中返回从数据库中获取的新对象

def editWithExecuteUpdate(id) {
    MerchantTier.executeUpdate('update MerchantTier mt set mt.value=:mrValue where mt.id=:mtId', [mrValue: 3.33.toDouble(), mtId: id.toLong()])
    merchantTier = MerchantTier.get(id)
}

一旦完成,您将在 when: 子句中拥有包含 MercerTier 对象的 testData

so in then: 子句有

 testData.value == 3.33

希望这是有道理的。谢谢

编辑 - 附加方式

def editWithExecuteUpdate(id) {
    def updatedRecords = MerchantTier.executeUpdate('update MerchantTier mt set mt.value=:mrValue where mt.id=:mtId', [mrValue: 3.33.toDouble(), mtId: id.toLong()])
    return updatedRecords
}

所以在 then: 子句中,由于 executeUpdate,只有一行应该根据唯一 id 更新,并且还应该再次获取新对象并检查持久值

 testData == 1
 def freshMerchantTier = MerchantTier.get(merchantTier.id)
 freshMerchantTier.value == 3.33

请尝试这种方式一次。谢谢

【讨论】:

  • 先生,这里的功能目标不是返回值,而是使用 exexuteUpdate() 检查数据是否已持久化,在上述情况下,首先我设置了给我对象的 MerchantTier,然后我可以访问每个属性,如merchanttier.value。其次,我在函数 editWithSetProperty() 中发送了 MercerTier.id 来编辑 MercerTier 的内容,但我没有返回并且它工作正常,但是当我尝试对 editWithExecuteUpdate() 做同样的事情时,它没有更新 MercerTier。
  • 注意:executeUpdate('delete query') 工作正常,再次没有问题 executeQuery('select query) 工作正常,但如果您不理解上述内容,则在 excuteUpdate('update') 内更新时会出现问题那么你可以建议我如何测试 executeUpdate(update query) 查询。谢谢。
  • 如果executeUpdate返回值gt 0,则表示该记录的数据已经持久化,您可以轻松检查,也可以通过再次获取对象来检查更新的值。
  • 是的,我在editWithExecuteUpdate() 中获取了merchantTier.get(id).value,但值没有保留。可能是什么原因?以及如何对包含 executeUpdate('update query here') 的函数进行测试。任何建议。谢谢
  • 尝试在更新查询和运行测试中对值和 id 进行一次硬编码。我猜它可能不会因为 toDouble() 和 toLong() 方法而持久化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
相关资源
最近更新 更多