Capybara: Trigger requests with custom request method

Updated . Posted . Visible to the public.

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
Last edit
Judith Roth
Keywords
delete, put, post
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2013-09-03 15:48)