ruby-on-rails - 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', '#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/

相关文章:

ruby-on-rails - 将 Guard::LiveReload 与 Rails 结合使用

ruby-on-rails - 有人可以向我解释一下这个 ruby​​ 语法是如何工作的吗?

ruby - 基于条件的 Rspec 测试

ruby-on-rails - capybara /rspec : should GET/PUT tests be in different files?

ruby-on-rails - caypbara/rspec/selenium-webdriver 无终端输出

ruby-on-rails - 模型未保存时在客户端获取错误文本

javascript - 使用 Turbolinks 完全加载后显示页面

ruby-on-rails - 使用rspec进行文件上传测试

ruby-on-rails - 更新 rspec gem 导致 'gem' 命令变得不稳定/有错误。帮助!

ruby - Capybara-webkit、rspec 集成规范和 xvfb : webkit_server: Fatal IO error: client killed