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

Ric*_*mas 2 rspec ruby-on-rails 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>
Run Code Online (Sandbox Code Playgroud)

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

<tr id="group_chair">
Run Code Online (Sandbox Code Playgroud)

单击"组主席"链接会使浏览器按预期向下滚动到正确的表行.现在我想制作一个测试此功能的规范.我尝试了以下方法:

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
Run Code Online (Sandbox Code Playgroud)

我希望这个测试能够通过,因为这是我点击右侧链接后浏览器的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
Run Code Online (Sandbox Code Playgroud)

知道这是怎么回事?

kla*_*eck 5

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

锚永远不会提交给服务器,所以从这个角度来看,current_url不包含Anchor是有意义的.我很害怕,我们可以从Capybara里面了解这种行为.我的猜测是问题与HTMLUnit一样远.

话虽如此,我个人从来没有在URL上断言任何东西,我发现这在集成测试中是不好的做法.这只是我的意见.

/乔纳斯

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