【问题标题】:Rspec have_content throwing error even when the content is present即使内容存在,Rspec have_content 也会抛出错误
【发布时间】:2018-07-18 13:48:33
【问题描述】:

我正在使用 cucumber 和 capybara、rspec 助手来编写测试用例。

当我使用 have_content 验证特定页面中是否存在文本时,即使页面中存在内容也会引发错误。

我的功能文件是

When I visit "/news"
Then I should not see a page with following content
  | content                                                                          |
  | .*Confirm your email address now.* |

步骤定义文件是

Then /^I should see a page with following content$/ do |table|
  table.hashes.each do |hash|
    page.should have_content(hash[:content])
  end
end

我得到的错误是

RSpec::Expectations::ExpectationNotMetError: 期望找到文本 “。立即确认您的电子邮件地址。”在“The North FaceBanana”中 共和国列维萨迪达斯真正的宗教J。拉尔夫的 CrewJordanPolo 劳伦儿童品牌GAPCCarter'sNikeChildren's Place GymboreeOshKosh B'goshConverse拉尔夫劳伦正义老海军人也 搜索维多利亚的秘密Christian LouboutinGoyardForever 21AsosParties它是如何工作的在 Poshmark 上销售新闻所有评论优惠想要 卖你的东西?在您的移动设备上下载 Poshmark!确认您的 现在的电子邮件地址,以确保您收到重要的订单和帐户 信息。”

我要查找的文本也出现在页面中。

如果我在某个地方错了,请帮助我..

提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby rspec cucumber capybara


    【解决方案1】:

    我发现我犯的错误了。

    have_content 将采用正则表达式,但为此您需要传递一个正则表达式 - page.should have_content(/.blah./)。如果你向它传递一个字符串,它会在相关元素内容上进行子字符串匹配,除非你传递确切的选项,然后它会使其完全匹配 element.should have_content('blah', exact: true)

    更改功能文件

    When I visit "/news"
    Then I should not see a page with following content
      | content                                                                          |
      | Confirm your email address now|
    

    而不是

    When I visit "/news"
    Then I should not see a page with following content
      | content                                                                          |
      | .*Confirm your email address now.* |
    

    纠正错误。

    【讨论】:

    • 注意:have_content 将采用正则表达式,但为此您需要将正则表达式传递给它 - page.should have_content(/.*blah.*/)。如果您将字符串传递给它,它将对相关元素内容进行子字符串匹配,除非您传递 exact 选项,这将使其完全匹配 element.should have_content('blah', exact: true)
    • @ThomasWalpole 非常感谢,我已在答案中添加了您的注释
    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多