Ruby on rails 水豚-如何测试链接到同一页的不同部分?

Ruby on rails 水豚-如何测试链接到同一页的不同部分?,ruby-on-rails,rspec,capybara,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', '#gr

在我的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>
我希望这个测试能够通过,因为这是我的浏览器的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) 集团官员职责页面应向下滚动至右侧部分
失败/错误:预期(当前url)。到eq“http://www.example.com/group_officer_duties#group_chair"
期望值:“http://www.example.com/group_officer_duties#group_chair"
得到:http://www.example.com/group_officer_duties"
(使用==进行比较)
#./spec/requests/group_officer_ducations_nav_spec.rb:9:in `区块(2层)'
以0.94885秒完成
1例,1例失败

你知道这是怎么回事吗?

不幸的是,我相信你运气不好——以下答案来自乔纳斯·尼古拉斯本人:

锚永远不会提交给服务器,因此从那时起 当然,当前的_url不包含 锚定。我们无法从内部了解这种行为 恐怕是水豚。我的猜测是,这个问题一直到现在为止 HTMLUnit

尽管如此,我个人从来没有在URL上声明过任何东西, 我发现在集成测试中这是一种糟糕的做法。那只是我的想法 尽管如此

/乔纳斯


因此,如果针对URL进行断言是一种不好的做法,那么针对路径进行断言是更好的做法吗?或者这个案例需要像Selenium这样的东西吗?我认为,他实际上的意思是,这是一个不应该在高级集成测试中测试的东西。相反,你应该检查你的观点,如所示(即使我不认为最好的做法)
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
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