Home > Enterprise >  Clicking a link with data-method="POST" in Capybara
Clicking a link with data-method="POST" in Capybara

Time:10-06

It seems it's not possible to send a POST-request with Capybara, even if that should be the direct result of a user action. For example, having a link with data-method="POST", and subsequently clicking that link raises a server error for me:

No route matches [GET] "blabla"

Which makes sense, given that it should be a PUT-request (as specified in its attributes). Found some related posts about Capybara's inability to directly send POST-requests, but none about links with the data-method attribute set.

CodePudding user response:

Using "data-method" relies on using the Rails Unobtrusive Javascript Driver to create a discrete form and post it to the server. If Rails UJS is not running properly or the client does not support JS a normal GET request will be sent instead - it is after all just a link and browsers send GET requests when you click links.

Capybara by default uses the RackTest driver which does not run javascript. Its not actually a real browser - its more like a browser simulator running on top of an XML parser.

You need to either use button_to instead which creates an actual form element and which does not rely on JS at all or change the Capybara driver to a driver that runs javascript such as Selenium Webdriver or Apparition.

  • Related