Search code examples
javascriptruby-on-rails-3capybaradestroy

Testing destroy methods with capybara


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


Solution

  • 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.