您如何在 Grails / Spock 测试中打印响应而不是:org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae

How do you print the response in a Grails / Spock test instead of: org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae

我正在为我的 Grails 后端编写 Spock 测试。我对在 Grails 中进行测试还很陌生,我正在尝试查看对我的模拟请求的响应。

当我写 println(response) 时,我在标准输出中看到了这个:org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae

而不是实际响应。是否可以查看此模拟 http 响应的内容而不是当前正在打印的内容?

只需使用Groovy的dump方法:

Generates a detailed dump string of an object showing its class, hashCode and fields.

println(response.dump())

我的例子:

def response = new GrailsMockHttpServletResponse()
println(response.dump())

输出:

<org.grails.plugins.testing.GrailsMockHttpServletResponse@6a79c292 outputStreamAccessAllowed=true writerAccessAllowed=true characterEncoding=ISO-8859-1 charset=false content= outputStream=org.springframework.mock.web.MockHttpServletResponse$ResponseServletOutputStream@21a947fe writer=null contentLength=0 contentType=null bufferSize=4096 committed=false locale=en_US cookies=[] headers=[:] status=200 errorMessage=null forwardedUrl=null includedUrls=[]>