【问题标题】:How to assert that some text only shows up on a web page certain number of times?如何断言某些文本仅在网页上出现一定次数?
【发布时间】:2013-09-18 20:14:50
【问题描述】:

对于我的 Rails 应用程序的一个集成测试 (minitest::spec),我想断言网页上显示一段文本的次数?例如,我想确保文本“This bit of text”。只在页面上显示一次。是否有我可以发送到 page.must_have_content 的其他参数?

page.must_have_content('This bit of text.')

或者其他类型的断言?谢谢。

【问题讨论】:

    标签: ruby-on-rails capybara integration-testing minitest


    【解决方案1】:

    您可以使用选项 :count、:between、:minimum 或 :maximum 来指定预期的匹配数。

    :count 选项指定匹配的确切数量。例如,以下要求页面中只有一次文本:

    page.must_have_content('This bit of text.', :count => 1)
    

    :between 选项指定了预期的匹配范围。例如,以下期望页面有 2 到 4 个匹配项:

    page.must_have_content('This bit of text.', :between => 2..4)
    

    :minimum 选项指定所需的最小匹配数。例如,以下要求有 2 个或更多匹配项。

    page.must_have_content('This bit of text.', :minimum => 2)
    

    :maximum 选项指定允许的最大匹配数。例如,以下预计最多有 2 个匹配项。

    page.must_have_content('This bit of text.', :maximum => 2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2016-11-17
      • 2012-03-29
      相关资源
      最近更新 更多