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
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
Share Improve this question asked May 3, 2011 at 14:55 AndreDuraoAndreDurao 5,7957 gold badges46 silver badges65 bronze badges2 Answers
Reset to default 4Rails 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.
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./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