Published by Simon Schoeters

Capybara POST requests

Capybara doesn't support POST requests, the built in visit method always uses GET. This is by design and with good reason: Capybara is built for acceptance testing and a user would never ask to “post” parameter X and Y to the application. There will always be some kind of interface, a form for example. So it makes more sence to simulate what the user would really do:

fill_in 'Login', :with => ''
fill_in 'Password', :with => 'password'
click_link 'Sign in'

Not convinced yet?

So far for the obligatory warning. I get the point, I even agree with the argument, but I still want to send a POST request. Why? Well, maybe you are testing an API without a user interface or you are writing some route tests? Is it really impossibe to simulate a POST request with Capybara? Nah, off course not.

it "registers a new temperature reading via the API" do'/temperature/new', { :params => { :degrees => "30" } }) 
  page.driver.status_code.should eql 200

Note that the second line is questionable as well. It asks the response status code from the driver but not every driver supports this. The previous snippet works with certain drivers but is not recommanded so use this snippet only when you understand why you can't use the first example.

This blog post is open source. Did you spot a mistake? Have any ideas for improvements? Contribute to this post via Github. Thank you!