【问题标题】:how to test for a presence of a text in a view page?如何测试视图页面中是否存在文本?
【发布时间】:2016-07-19 07:19:35
【问题描述】:

在进行集成测试时,Grails 是否有任何方法可以断言视图页面中存在文本?

例如:有时我们想要断言视图页面包含某些元素。例如:在电影列表页面中,我们要断言给定的电影显示在视图页面中。我们如何在 Grails 中有效地实现这一点?谢谢!

【问题讨论】:

    标签: grails integration-testing


    【解决方案1】:

    好吧!据我所知,当我们渲染视图时,我们会得到 String 作为回报,因此您可以使用 String 的 contains 方法来测试页面是否包含一些文本。

    这样的例子很多。

    以下是http://mrhaki.blogspot.in/2013/05/grails-goodness-testing-views-and.html博客的一篇:

    package com.grails.views
    
    import grails.test.mixin.TestMixin
    import grails.test.mixin.web.GroovyPageUnitTestMixin
    import spock.lang.Specification
    
    @TestMixin(GroovyPageUnitTestMixin)
    class HeaderTemplateSpecification extends Specification {
    
        def "if username is set then show message Hi with username"() {
            expect: 'Template output must contain Hi, Johnson'
           (render(template: '/sample/header', model: [username: 'Johnson'])).contains 'Hi, Johnson'
        }
    
    }
    

    以上,如果您注意到已呈现 gsp 视图并检查视图中是否存在 Hi,mrhaki 字符串。

    希望它能回答你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      相关资源
      最近更新 更多