Rspec have_content 即使内容存在也抛出错误

Rspec have_content throwing error even when the content is present

我将黄瓜和水豚一起使用,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: expected to find text ".Confirm your email address now." in "The North FaceBanana RepublicLevi'sadidasTrue ReligionJ. CrewJordanPolo by Ralph LaurenKids' BrandsGAPCarter'sNikeChildren's PlaceGymboreeOshKosh B'goshConverseRalph LaurenJusticeOld NavyPeople Also SearchedVictoria’s SecretChristian LouboutinGoyardForever 21AsosPartiesHow it worksSell on PoshmarkNewsAllCommentsOffersWant to sell your stuff? Download Poshmark on your mobile device!Confirm your email address now to make sure you receive important order and account information."

我要找的文字也在页面中。

如果我哪里错了请帮助我..

提前致谢。

我发现了我犯的错误。

have_content 将采用正则表达式,但为此您需要将正则表达式传递给它 - page.should have_content(/.blah./).如果您向它传递一个字符串,它会对相关元素内容进行子字符串匹配,除非您传递 exact 选项,这将使它进行精确匹配 element.should have_content('blah', exact : 正确)

正在更改功能文件

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.* |

纠正错误。