在我的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">
单击“组主席”链接会使浏览器按预期向下滚动到正确的表格行。现在,我想制定一个测试该功能的规格。我尝试了以下方法:
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
知道这是怎么回事吗?
不幸的是,我相信您不走运-以下答案来自乔纳斯·尼克拉斯本人:
锚永远不会提交给服务器,因此从该角度来看,current_url不包含锚是有意义的。恐怕在水豚内部我们对这种行为知之甚少。我的猜测是问题远不及HTMLUnit。
话虽这么说,我个人还是从不对URL声明任何内容,我发现在集成测试中这是一种不好的做法。不过那只是我的观点。
/乔纳斯
https://groups.google.com/forum/#!topic/ruby-capybara/KMEWM8nuZlE
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句