GORM的此实现不支持findAll

 觴儿_996 发布于 2023-01-07 21:38

我正在尝试测试服务中的一种方法,但是我不断收到错误消息

GORM的此实现当前不支持基于字符串的查询,例如[findAll]。请改用条件。

我的服务:

class MyService {
  List colorShade (String shade) {
    def shadeId = Shade.findByName(shade).id
    return ColorShade.get(shadeId)      
  }
}

我的班级ShadeColor领域

class ColorShade extends Serializable {
  Color color
  Shade shade
  static ColorShade create (Color color, Shade shade, boolean flush = false) {
    if (get(shade.id) == null)
        new ColorNetwork(color: color, shade: shade).save(flush:  flush)
  }

   static ColorShade get (long shadeId) {
        ColorShade.findAll 'from ColorShade where shade.id = :shadeId',
                [shadeId: shadeId]
    }
    static mapping = {
        table 'color_shade'
        version false
        id composite: ['color', 'shade']
    }
}

我的规格:test\unit\MyServiceSpec.groovy我也尝试将其添加到test\integration\MyServiceSpec.groovy

@TestFor(MyServiceSpec)
@Mock([Color, Shade, ColorNetwork])
@TestMixin(DomainClassUnitTestMixin)
class MyServiceSpec extends Specification {

    def shade
    def color
  def setup(){
    color = new Color(name: "red")
    shade = new Shade(name: "dark")
    color.save(flush: true)
    shade.save(flush: true)

    ColorShade.create(color, shade)
    /* 
    I've also tried:
    mockDomain(ColorShade, [[color: color, shade: shade]])
    but I get the same error
    */

  }
  def cleanup () {
  }

  def "test colorShade" () {
    expect:
       1 == service.colorShade("dark").size()
  }
}

当我运行此测试时,出现错误:

String-based queries like [findAll] are currently not supported in this implementation of GORM. Use criteria instead.

我该如何测试?我在grails 2.2.4上

注意:这只是我针对该问题而创建的示例测试方案。我的实际测试有所不同,但在其中我遇到了与此问题相同的问题。

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有