【问题标题】:Testing rendered content of a .gsp page in Grails 2.3.8在 Grails 2.3.8 中测试 .gsp 页面的渲染内容
【发布时间】: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/…

标签: grails testing


【解决方案1】:

响应未在单元测试中完全呈现。 response.text will always be null when rendering views.

单元测试的目的是测试一个单元并模拟它周围的一切。视图层在该单元之外。

如果你想测试视图,试试GroovyPageUnitTestMixin

先生。 Haki 有一个 good post on how to test views and templates 解释如何测试它们。

【讨论】:

    【解决方案2】:

    @ColinHarrington 感谢您的回答,抱歉耽搁了。我已经尝试过它在这些网站上所说的内容,但是无论我呈现什么视图 (.gsp)(就像一个只呈现变量内容的简单页面),render 方法总是返回 null(甚至不是空字符串)。

    控制器:

    class TestingController {
    
        def index() {
            render view: "testing.gsp", model: [test:"Test String"]
        }
    }
    

    测试:

    import grails.test.mixin.TestMixin
    import grails.test.mixin.web.GroovyPageUnitTestMixin
    import spock.lang.Specification
    
    
    @TestMixin(GroovyPageUnitTestMixin)
    class TestingControllerSpec extends Specification {
        def controller
    
        def setup() {
            controller = testFor(TestingController)
        }
    
        void "test something"() {
            when:
                controller.index()
            then:
                render (view:"/testing/testing.gsp", model:model) == "Test String"
        }
    }
    

    测试结果:

    test something(webbluefinder.TestingControllerSpec)
     |
    Condition not satisfied:
    render (view:"/testing/testing.gsp", model:model) == "Test String"
    |                                          |      |
    null                                       |      false
                                           [test:Test String]
    

    我也尝试了Haki先生的方法,得到了同样的结果。 你碰巧知道为什么会这样吗?感谢您的宝贵时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2023-04-03
      • 2016-04-24
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多