Grails 2.3.6集成规范Spock测试can';t使用@Stepwise时修改controller.request.JSON
我在Grails2.3.6中有一个Grails 2.3.6集成规范Spock测试can';t使用@Stepwise时修改controller.request.JSON,grails,grails-2.0,spock,Grails,Grails 2.0,Spock,我在Grails2.3.6中有一个IntegrationSpec测试,它创建任何控制器的实例,将数据添加到主体中(通过controller.request.JSON),然后验证它是否正确设置 问题是,当我逐步添加@注释时,它似乎将请求对象锁定在控制器对象上。在调试器中,我看到它是同一个对象(基于hashcode),正如下面的测试失败所示,第二次使用where:块中的值运行测试时,它失败了,因为where:块中的第一个值仍然存在 @Stepwise class TestSpec extends I
IntegrationSpec
测试,它创建任何控制器的实例,将数据添加到主体中(通过controller.request.JSON
),然后验证它是否正确设置
问题是,当我逐步添加@注释时,它似乎将请求
对象锁定在控制器
对象上。在调试器中,我看到它是同一个对象(基于hashcode),正如下面的测试失败所示,第二次使用where:
块中的值运行测试时,它失败了,因为where:
块中的第一个值仍然存在
@Stepwise
class TestSpec extends IntegrationSpec {
@Unroll
void "changing controller request"() {
setup:
SomeController controller = new SomeController()
when:
controller.request.JSON = json
then:
controller.request.JSON == json
where:
json << [
[one: "1"],
[two: "2"]
]
}
}
如果我逐步删除@
,此操作不会失败
有没有办法强制重新创建请求
对象,或者覆盖以前测试设置的值?永不失败-一个问题困扰了我好几个小时,我在这里发布它,然后找出解决方案
所以我研究了IntegrationSpec
,它是一个特定于grails的类,用于在集成环境中处理Spock规范。我注意到setupSpec()
和setup()
方法将通过调用initRequestEnv()
来初始化请求对象。不幸的是,只有当这不是一个逐步进行的测试时才会发生这种情况-我不知道为什么
解决方案是在测试中手动添加此检查,方法是在测试中添加以下行,这将为每个测试重新创建request
对象
private GrailsTestRequestEnvironmentInterceptor perMethodRequestEnvironmentInterceptor = null
def setup() {
perMethodRequestEnvironmentInterceptor = initRequestEnv()
}
def cleanup() {
perMethodRequestEnvironmentInterceptor?.destroy()
}
我相信特性方法中cleanup:中的controller.response.reset()也能解决这个问题。
private GrailsTestRequestEnvironmentInterceptor perMethodRequestEnvironmentInterceptor = null
def setup() {
perMethodRequestEnvironmentInterceptor = initRequestEnv()
}
def cleanup() {
perMethodRequestEnvironmentInterceptor?.destroy()
}