How do you POST to a URL in Capybara?

前端 未结 8 668
无人及你
无人及你 2020-11-28 09:51

Just switched from Cucumber+Webrat to Cucumber+Capybara and I am wondering how you can POST content to a URL in Capybara.

In Cucumber+Webrat I was able to have a ste

8条回答
  •  天涯浪人
    2020-11-28 10:19

    If your driver doesn't have post (Poltergeist doesn't, for example), you can do this:

    session = ActionDispatch::Integration::Session.new(Rails.application)
    response = session.post("/mypath", my_params: "go_here")
    

    But note that this request happens in a new session, so you will have to go through the response object to assert on it.

    As has been stated elsewhere, in a Capybara test you typically want to do POSTs by submitting a form just like the user would. I used the above to test what happens to the user if a POST happens in another session (via WebSockets), so a form wouldn't cut it.

    Docs:

    • http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html
    • http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html

提交回复
热议问题