水豚:点击图标 class 找到的元素

Capybara: click on element found by the icon class

我想检查一下,当单击图标 link 时,current_path 是 'show' 操作的路径。

因此,鉴于此 HTML:

<a class="btn btn-xs" href="/admin/hotels/1">
   <i class="fa fa-eye fa-lg"></i>
</a>

还有这个水豚测试:

describe "GET /hotels", js:true do
  before :each do
    @hotel = create(:hotel)
    visit admin.hotels_path
  end
  it "eye icon links to show" do
    find(:css, 'i.fa.fa-eye.fa-lg').find(:xpath,".//..").click
    expect(current_path).to eq(admin.hotel_path(@hotel))
  end
end

我收到此错误消息:

   1) Hotels index hotel creation GET /hotels eye icon link to show
      Failure/Error: expect(current_path).to eq(admin.hotel_path(@hotel))

   expected: "/admin/hotels/560bb674467261c7a4000002"
        got: "/admin/hotels"

我推断 find(:css, 'i.fa.fa-eye.fa-lg').find(:xpath,".//..").click 没有像我预期的那样工作。

find(:css, 'i.fa.fa-eye.fa-lg').find(:xpath,".//..") 的执行正在返回:

=> #<Capybara::Element tag="a">

我是不是做错了什么?

问题是:

describe "GET /hotels", js:true do

我已将其替换为:

describe "GET /hotels" do

然后它使用 poltergeist 很好,没有错误。

测试失败并显示 :js 的原因是因为单击 link 只能做到这一点。它不会等待点击的结果(加载新页面)。因为在仍然获得原始路径之后立即调用 current_path 。删除 :js 会停止使用 poltergeist,而是使用同步但缺少很多真实浏览器行为的机架测试。在 capybara 2.4 中,您需要检查显示页面上的内容,一旦内容出现就获取当前路径,但是 capybara 2.5 具有 have_current_path 匹配器,它将利用 capybaras 等待行为。也不需要点击 a 元素,像用户那样点击它的内容应该就可以了。所以

find(:css, 'i.fa.fa-eye.fa-lg').click
expect(page).to have_current_path(admin.hotel_path(@hotel))

在有和没有 :js 的情况下都应该工作