问题
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')
??
回答1:
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
来源:https://stackoverflow.com/questions/33429800/executeupdate-not-updating-on-grails-spock-integration-testing