Capybara: Trigger requests with custom request method

Preface: Normally, you would not need this in integrations tests (probably that's why it is so hard to achieve), because it is no actual "integration testing". If you use this step, know what you are doing.


Destroying a record with Capybara is not as easy as calling visit user_path(user, method: :delete), because RackTest's visit can only perform GET requests Show archive.org snapshot .

With this step you can destroy a records using either Selenium or RackTest. Example: When I destroy that user # => deletes User.last.

When /^I destroy that (.*)$/ do |element_type|
  element = element_type.classify.constantize.last
  path = "#{element_type}_path"

  case page.driver
  when Capybara::RackTest::Driver
    page.driver.submit :delete, send(path, element), {}
  else # e.g. Capybara::Selenium::Driver
    visit send(path, element, { method: :delete })
  end
end
Dominik Schöler