有一些控制器:
def index() {
...
render(view: 'index', model: [exceptions: exceptions]
}
还有单元测试:
def testIndex(){
...
controller.index()
assert controller.response.status == 200
assert controller.response.contentAsString != ""
}
第二个断言失败,因为contentAsString
返回“”。当我用render()
替换我的render("html")
- contentAsString
即使在测试中也已完成。我做错了什么?
更新1
index.gsp中
<%@ page contentType="text/html"%>
<%@ page import="org.apache.commons.lang.exception.ExceptionUtils" %>
<html>
<head>
<title>Exceptions catched during crawling</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(".stacktrace").hide()
$("a").click(function(){
$(this).parents("li").siblings("pre").toggle()
return false;
})
})
</script>
</head>
<body>
<g:each var="entry" in="${exceptions.entrySet()}">
Catched <b>${entry.key.name}</b> for these URLs:
<ol>
<g:each var="ex" in="${entry.value.entrySet()}">
<li><a href="${ex.key}">${ex.key}</a>: ${ex.value.message}</li>
<pre class="stacktrace">
${ExceptionUtils.getStackTrace(ex.value)}
</pre>
</g:each>
</ol>
</g:each>
</body>
</html>
更新2 从grails 2.2.3升级到grails 2.2.4无济于事;(
答案 0 :(得分:2)
好的,我重新访问了文档的Unit Testing部分。似乎只有在直接渲染文本时才能使用response.contentAsString
。要渲染视图,可以检查文档的“测试视图渲染”项。
示例:
// Test class
class SimpleController {
def home() {
render view: "homePage", model: [title: "Hello World"]
}
…
}
void testIndex() {
controller.home()
assert view == "/simple/homePage"
assert model.title == "Hello World"
}