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

标签 grails gorm

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

更奇怪的是,当我通过浏览器正常运行代码时,它可以完美运行。我可以在数据库中查找并看到该字段正在保留。只是集成测试不起作用。这一定是带有脏字段的某种休眠 session 问题。

这是我的简化代码。我省略了 Controller 信息。我的测试调用了 Controller , Controller 调用了服务。

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()之后,标题字段应该具有有效数据,但它仍为null(创建实例时默认)。

最佳答案

这可能是您的域实例的缓存,请尝试以下操作:

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
  }
}

关于grails - grails executeUpdate集成测试中的刷新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19732595/

相关文章:

forms - Grails Command Object与模型重定向以具有相同的url/页面

Grails:404 和延迟初始化

grails - 如何遍历Groovy中的列表并为每个对象添加属性

grails - grails什么时候分配ID?

grails - 在代理后面滚动/重定向到https

grails - Gorm Findallby日期提示

grails - 使用 Spring Security 处理成功登录事件

unit-testing - 如何编写Grails delete()方法的测试

grails - 如何打破继承

grails - Grails的域read()函数是否使用二级缓存?