【问题标题】:RSpec: should have_content can't find textRSpec:应该有_内容找不到文本
【发布时间】:2014-05-16 13:32:01
【问题描述】:

我有以下页面

<ul class="account_menu dropdown-menu">
    <li class="divider"></li>
    <li class="current"><span>My account</span></li>
    <li class="account"><a href="#">account-1 (Editor)</a></li>
</ul>

使用此步骤用 Cucumber 对其进行测试

within(scope) do
    page.should have_content(content)
end

测试在 content = 'account-1' 时通过,而在 content = 'My account' 时失败。范围是两种情况都是'account_menu'

And he should see "My account" within account menu

expected to find text "My account" in "My Accountaccount-1 (Editor)"
(RSpec::Expectations::ExpectationNotMetError)
  • 导轨 3.2.17
  • rspec-rails 2.14.1
  • 水豚2.1.0

为什么找不到“我的帐户”文本?谢谢

【问题讨论】:

  • 你正在使用within(scope);你申请的是什么范围?
  • 范围是 account_menu
  • 大写?帐户中的“A”在页面中大写,而不是在您的测试中。我不认为默认的正则表达式不区分大小写,所以它不匹配。
  • 在测试和“我的帐户”页面中
  • 我可以在“My Accountaccount-1 (Editor)”中清楚地看到“我的帐户” - 从错误消息中 - 尽管 RSpec 由于某种原因不能

标签: ruby-on-rails rspec cucumber


【解决方案1】:

have_content 有时会以意想不到的方式连接内容。你的情况

<li class="current"><span>My account</span></li>
<li class="account"><a href="#">account-1 (Editor)</a></li>

成为“My Accountaccount-1 (Editor)”

如果你尝试

expect("My Accountaccount-1 (Editor)").to have_content("My account")

它也会失败,这在技术上是正确的,因为“我的帐户”不在字符串中。另一种方法是使用match 匹配器或测试连接的字符串(丑陋)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2021-07-13
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    相关资源
    最近更新 更多