Capybara-如何测试链接到同一页面的不同部分?

时间:2013-09-22 20:58:05

标签: ruby-on-rails rspec capybara

在我的Rails应用中,我在页面顶部有以下链接:

<ul>
  <li><%= link_to 'Group Chairperson', '#group_chair' %></li>
  <li><%= link_to 'Group Treasurer', '#group_treasurer' %></li>
  <li><%= link_to 'Group Secretary', '#group_secretary' %></li>
</ul>

在页面的下方,我有以下表格行选择器:

<tr id="group_chair">

点击&#39;小组主席&#39;链接导致浏览器按预期向下滚动到正确的表行。现在我想制作一个测试此功能的规范。我尝试了以下方法:

require 'spec_helper'

describe "Group officer duties page" do

  before { visit group_officer_duties_path }

  it "should scroll down to the right section" do
    click_link "Group Chairperson"
    expect(current_url).to eq "http://www.example.com/group_officer_duties#group_chair"
  end

end

我希望此测试能够通过,因为这是我点击右侧链接后浏览器的URL栏所显示的内容。但是,我实际上得到了这个:

Failures:

  1) Group officer duties page should scroll down to the right section
     Failure/Error: expect(current_url).to eq "http://www.example.com/group_officer_duties#group_chair"

       expected: "http://www.example.com/group_officer_duties#group_chair"
            got: "http://www.example.com/group_officer_duties"

       (compared using ==)
     # ./spec/requests/group_officer_duties_nav_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.94885 seconds
1 example, 1 failure

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

不幸的是,我相信你运气不好 - 以下答案来自Jonas Nicklas本人:

  

锚永远不会提交给服务器,所以从那时起     对于current_url来说,不包括它是有意义的     锚。从内部我们几乎无法了解这种行为     水豚,我很害怕。我的猜测是这个问题远远低于     的HtmlUnit。

     

话虽如此,我个人从未在URL上断言任何东西,     我发现在集成测试中这是不好的做法。那只是我的     然而,意见。

     

/纳斯

https://groups.google.com/forum/#!topic/ruby-capybara/KMEWM8nuZlE