bir*_*rdy 5 grails unit-testing spock
我正在尝试测试服务中的一种方法,但是我不断收到错误消息
GORM的此实现当前不支持基于字符串的查询,例如[findAll]。请改用条件。
我的服务:
class MyService {
List<Color> colorShade (String shade) {
def shadeId = Shade.findByName(shade).id
return ColorShade.get(shadeId)
}
}
Run Code Online (Sandbox Code Playgroud)
我的班级Shade和Color领域
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']
}
}
Run Code Online (Sandbox Code Playgroud)
我的规格: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()
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,出现错误:
String-based queries like [findAll] are currently not supported in this implementation of GORM. Use criteria instead.
Run Code Online (Sandbox Code Playgroud)
题
注意:这只是我针对该问题而创建的示例测试方案。我的实际测试有所不同,但在其中我遇到了与此问题相同的问题。
如果您使用的是 4.3.5.4 或更高版本的 hibernate4 插件,那么您可以使用grails.test.mixin.hibernate.HibernateTestMixin,它在您的单元测试环境中支持完整的 Hibernate GORM。您需要将其添加为依赖项,并在grails-app/conf/BuildConfig.groovy.
dependencies {
test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
}
Run Code Online (Sandbox Code Playgroud)
然后用注释来注释您的测试grails.test.mixin.hibernate.HibernateTestMixin。
| 归档时间: |
|
| 查看次数: |
1101 次 |
| 最近记录: |