在我的 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">
单击“Group Chairperson”链接会使浏览器按预期向下滚动到正确的表格行。现在我想制定一个测试此功能的规范。我尝试了以下方法:
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
知道这是怎么回事吗?
最佳答案
不幸的是,我相信你运气不好 - 以下答案来自 Jonas Nicklas 本人:
the anchor is never submitted to the server, so from that point of view, it kind of makes sense for current_url to not include the Anchor. There's very little we can about that behaviour from inside Capybara, I'm afraid. MY guess is that the issue goes as far down as HTMLUnit.
That being said, I personally never assert anything on the URL anyway, I find that it's bad practice in integration tests. That's just my oppinion though.
/Jonas
https://groups.google.com/forum/#!topic/ruby-capybara/KMEWM8nuZlE
关于ruby-on-rails - Capybara-如何测试指向同一页面不同部分的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948687/