给定一个带注入服务的简单域,用于在约束内执行某些验证,如:
package org.example.domain
class Ninja {
String name
String sensei
String village
def ninjaService
static transients = ['ninjaService']
static constraints = {
....
village nullable:true, validator:{ val, obj, errors ->
obj.ninjaService.validate(obj)
}
}
}
一个简单的Spec,它存储服务行为,例如:
package org.example.domain
import grails.test.mixin.Mock
import spock.lang.Specification
import grails.test.mixin.TestFor
import org.example.services.NinjaService
@TestFor(Ninja)
class NinjaSpec extends Specification {
def 'Should succeed at validating the village using a stubbed method'() {
given:
def instance = new Ninja(village: 'Leaf')
instance.ninjaService = Mock(NinjaService)
when:
instance.validate(['village'])
then:
1 * instance.ninjaService.validate(instance) >> { final Ninja ninja ->
ninja.errors.rejectValue 'village', 'should.be.an.error', [].toArray(), null
ninja.log.debug "[NINJA SERVICE MOCK] (VALIDATING) 'village' FIRED! ninja.errors['village']?.code: '${ninja.errors['village']?.code}'"
}
and:
instance.errors['village']?.code == 'should.be.an.error'
}
}
然后,会发生的事情是instance.errors [' village']?。代码为空。 看看:
grails test-app unit:spock -clean -echoOut NinjaSpec
| Compiling 121 source files
| Running 1 spock test... 1 of 1
--Output from Should succeed at validating the village via mock--
2016-02-04 19:51:00.219 [main] grails.app.domain.org.example.domain.Ninja
DEBUG [NINJA SERVICE MOCK] (VALIDATING) 'village' FIRED! ninja.errors['village']?.code: 'should.be.an.error'
| Failure: Should succeed at validating the village via mock(org.example.domain.NinjaSpec)
| Condition not satisfied:
instance.errors['village']?.code == 'should.be.an.error'
| | | | |
| | null null false
| org.grails.datastore.mapping.validation.ValidationErrors: 0 errors
org.example.domain.Ninja : (unsaved)
at org.example.domain.NinjaSpec.Should succeed at validating the village via mock(NinjaSpec.groovy:36)
| Completed 1 Spock test, 1 failed in 2032ms
为什么实例不包含已在存根交互中设置的代码 should.be.an.error ?
答案 0 :(得分:0)
因为看起来你没有使用服务而是模拟,所以它可能没有验证任何东西。尝试创建服务的实例而不是模拟它。
答案 1 :(得分:0)
@PabloPazos使用mockFor()和createMock()建议的实现就像一个魅力!
def 'Should succeed at validating the village via mock'() {
given:
def instance = new Ninja(village: 'Leaf')
def ninjaServiceMock = mockFor(NinjaService)
ninjaServiceMock.demand.validate { final Ninja ninja ->
ninja.errors.rejectValue 'village', 'should.be.an.error', [].toArray(), null
}
instance.ninjaService = ninjaServiceMock.createMock()
when:
instance.validate(['village'])
then:
instance.errors['village']?.code == 'should.be.an.error'
}
但我仍然不明白,为什么Spock存根服务不起作用。