单击Capybara中的元素(不是链接或按钮)

时间:2018-03-07 05:49:45

标签: ruby-on-rails capybara poltergeist

我正在进行集成测试,其中我需要单击一个html类元素,它将使用Ajax转到控制器操作。

这是我的html slim代码:

table.table-default
 thead
   tr Title
 tbody
   tr.title
     td Hello World

JavaScript处理click

上的tr事件
$(function() {
   $('.title').click(function() {
      $.ajax({
        type: 'GET',
        url: "/admin/dashboards/load_info/
      });
   })
})

我在控制器操作中引发了一个错误,以测试它是否会转到控制器

class Admin::DashboardsController < Admin::BaseController
  def load_info
    raise 'hello'.inspect
  end
end

当我在浏览器上测试时,它会引发hello

这是我目前的测试设置:

gem "minitest-rails"

group :test do
  gem 'shoulda', '~> 3.5'
  gem 'shoulda-matchers', '~> 2.0'
  gem 'rails-controller-testing'
  gem 'minitest-spec-rails'
  gem 'm', '~> 1.5.0'
  gem 'minitest-rails-capybara'
  gem 'database_cleaner'
  gem 'phantomjs', :require => 'phantomjs/poltergeist'
  gem 'poltergeist'
end

我的测试服是这样的:

class DashboardFlowTest < Capybara::Rails::TestCase

   before(:all) do 
      Capybara.current_driver = :poltergeist
   end

   test "Given I'm at dashboard page when I click the table row it should redirect me to load_info' do 
      visit 'admin/dashboards'

      find('.title').click

      assert_current_path admin_dashboards_load_info_path
   end

end

我得到的错误是:

Capybara::Poltergeist::StatusFailError: Request to 'http://127.0.0.1:50102/admin/dashboards/' failed to reach server, check DNS and/or server status - Timed out with no open resource requests

当我运行测试时,它不会引发错误。我怀疑测试中的click函数仅支持链接或按钮,或者我的测试未启用JavaScript。

0 个答案:

没有答案