executeUpdate()不在grails spock集成测试上更新 [英] executeUpdate() not updating on grails spock-integration testing

查看:258
本文介绍了executeUpdate()不在grails spock集成测试上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是Grails测试新手。愿意做如下集成测试,但
问题是 executeUpdate()似乎没有更新值

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



请帮助建议我
示例代码给出问题演示。
提前致谢。

  def给定的商家级别更新商家级别值(){
setup :
def merchantTier = new MerchantTier(
值:1.11).save(flush:true)//它保存商家

当:使用setProperty更新时
testData = editWithSetProperty(merchantTier.id)// id传递

然后:它更新数据并测试成功
merchantTier.value == 2.22

当:使用executeUpdate查询时
testData = editWithExecuteUpdate(merchantTier.id)// id传递

然后:它不更新数据并且测试失败
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 =:mrV alue where mt.id =:mtId',[mrValue:3.33.toDouble(),mtId:id.toLong()])
}

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

解决方案

在通过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)
}

一旦完成,那么您将在time:子句中包含merchantTier对象的testData

那么:子句有

  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()])
返回updatedRecords
}

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

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



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

hi i am new to grails testing. Willing to do integration test as below but problem is that executeUpdate() seems not updating the value

How to do integration testing for executeUpdate('update query goes here') ??

Please help suggest me Sample code is given for problem demo. Thanks in advance.

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()])
}

How to do integration testing for executeUpdate('update query goes here') ??

解决方案

as you are updating through executeUpdate you again need to fetch the object from database so try returning the fresh object fetched from database in 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)
}

once done then you will have testData containing merchantTier object in when: clause

so in then: clause have

 testData.value == 3.33

hope this make sense. Thanks

Edit - Additional Way

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
}

so in then: clause have as due to executeUpdate only one row should be updated based on unique id and also fetch fresh object again and check the persisted value

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

Try this way once, please. Thanks

这篇关于executeUpdate()不在grails spock集成测试上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆