【发布时间】:2014-09-04 15:29:12
【问题描述】:
第一次在这里问。我会直奔主题。我正在尝试测试控制器的渲染视图,但我还没有找到任何明确的方法来做到这一点。特别是,我要做的是测试 .gsp 呈现的实际内容;即,如果呈现的 html 页面包含项目列表(列表,例如在 <ul> 和 <li> 标记中),或页面应显示的任何其他内容。这可能吗?甚至是测试这种事情的有效方法?我试着做这样的事情:
控制器:
class ConnectedPairController {
def show(int id) {
//Some logic to populate collections and stuff
render(view:"show.gsp", model:['pqc':cp.getPath(),
'connected':cp.getPage(),
'instance':coll,
'pagesCollection':colPages])
}
测试:
@TestFor(ConnectedPairController)
class ConnectedPairControllerSpec extends Specification {
void "test show"() {
when:
controller.show(4) //4 being the id of the content I want to display
then:
controller.response.text.contains "Some string or html tag the page should display"
}
}
但返回的文本为空。也许这就是它应该的样子,但是有没有办法获取渲染的内容?
我在文档中发现的东西没有用(至少对于我想做的事情没有用),因为恕我直言,它只测试琐碎的东西,比如正在渲染的 .gsp 或变量的内容:
@TestFor(SimpleController)
class SimpleControllerSpec extends Specification {
void 'test home'() {
when:
controller.home()
then:
view == '/simple/homePage'
model.title == 'Hello World'
}
}
我希望我说得足够清楚。感谢您的宝贵时间。
PS:我使用的是 Grails 2.3.8。如果您需要任何其他与环境等相关的信息,请告诉我。
【问题讨论】:
-
如果我今天晚些时候有更多时间,我会把这个链接变成一个答案。同时,试试这个方法:grailstraining.com/groovy/…