2

I've written some tests using Capybara, but I'm not using selenium and neither any other JS drivers. But my doubt is if I can test a destroy method this way? Since I need to confirm a JS confirmation and the data-method = "delete" can't be visited...

I would like to do something very Capybara's way like:

visit '/people/123', :data-method => 'delete'

Do you guys know if is there some way to do that?

Thanks in advance, Andre

AndreDurao
  • 5,600
  • 7
  • 41
  • 61

2 Answers2

4

Just something to be aware of with @Mike Mazur's answer, if your controller does a redirect on the #destroy action, invoking the delete method on Capybara's driver doesn't actually follow the redirect. Instead it just populates the response with the redirection message, i.e. body == <html><body>You are being <a href=\"http://www.example.com/model_names\">redirected</a>.</body></html> and status_code == 302.

So, to get everything populated like you would expect on a normal Capybara click_button 'Delete Model', you'll need to manually follow the redirect. One (unrefactored and RSpec flavored) way to do this:

Capybara.current_session.driver.delete controller_path(model_instance)
Capybara.current_session.driver.response.should be_redirect
visit Capybara.current_session.driver.response.location
DangerDave
  • 1,837
  • 1
  • 11
  • 7
  • 1
    wow this is perfect. Thanks. Any reason you are specifying Capybara.current_session vs just using page? – davekaro Feb 28 '13 at 21:22
4

Rails has JavaScript code which generates a form from the link's href and data-method attributes and submits it; this won't work without JS.

One way to test this: first, test for the presence of the link and proper attributes (href, data-method), then trigger the delete request manually with the Capybara::RackTest::Driver#delete method. If you do this often, write a helper method wrapping those two steps.

Mike Mazur
  • 2,509
  • 1
  • 16
  • 26
  • Thanks for your answer. Besides that I found a blog post about the same problem (http://blog.ardes.com/2010/4/28/capybara-and-rack-test-sessions-and-http-methods) and he managed to solve it with the lines: rack_test_session_wrapper = Capybara.current_session.driver rack_test_session_wrapper.process :delete,mycontrollerpath(@item) – AndreDurao May 09 '11 at 20:35