grails executeUpdate集成测试中的刷新错误

时间:2013-11-01 18:02:35

标签: grails gorm

我有一个更新db列的服务。更新使用executeUpdate完成。在我的服务方法测试后,我尝试加载记录。记录加载并填充每个字段,除了我刚刚在服务中更新的字段。

为了让它更奇怪,当我通过浏览器正常运行代码时,它可以完美地运行。我可以查看数据库,看看该字段是否持久化。它只是集成测试,不起作用。它必须是脏字段的某种类型的休眠会话问题。

这是我的精简代码。我遗漏了控制器信息。我的测试调用控制器,控制器调用服务。

class BasicProfile {
   static hasMany = [ photos:Photo ]
}

class Photo {
   BasicProfile basicProfile
    String caption
    String publicId

   static belongsTo = [ basicPofile:profile ]
}


class PhotoService {

   def updateCaption() {
       ...
        Photo.executeUpdate("update Photo p set p.caption = ? where p.basicProfile.id = ? and p.publicId = ? ",
            [newCaption, profile.id, publicId])
        ...
    }
}


void testUpdateCaption() {
   ... 
    controller.updateCaption() //controller just calls the photoService method
    //get json result from controller to load publicId
    ...
    Photo photo = Photo.findByPublicId(publicId)

    assertEquals "my new caption", photo.caption  //photo.caption is null, but the rest of the photo object is populated properly from the database load
}

我在断言上添加了断点,以便查看照片实例。它是一个有效的实例,每个字段都填充了创建时的数据(在调用controller.updateCaption()之前)。但是在调用controller.updateCaption()之后,标题字段应该有有效的数据,但它&# 39; s仍然为null(创建实例时的默认值)。

1 个答案:

答案 0 :(得分:1)

这可能是你的域名实例的缓存,试试这个:

void testUpdateCaption() {
  controller.updateCaption()
  //force the query in a clean hibernate session
  Photo.withNewSession {
    def photo = Photo.findByPublicId(publicId)
    assertEquals "my new caption", photo.caption
  }
}