How to test post request and follow redirects with capybara?

妖精的绣舞 提交于 2019-12-11 20:16:40

问题


I'm trying to test a post request with capybara and cucumber. I can't visit a page and press a link to send the request because it has a confirm dialogue and I haven't made it work with javascript so I'm trying to use the post method and then test if it redirects to the appropriated url but I'm getting:

No response yet. Request a page first. (Rack::Test::Error)

My code is:

page.driver.post url
page.driver.status_code.should be 302
follow_redirect!

It's working fine until follow_redirect!. I've tried page.driver.follow_redirects! but it doesn't work either.

Any help?


回答1:


Capybara::Rails::TestCase.class_eval do
  def post(url)
    page.driver.post url
    visit page.driver.response.location
  end
end



回答2:


I would recommend you to dismiss the confirm dialogue by using this:

https://stackoverflow.com/a/2609244/560123

That way you wouldn't care about making the post by yourself, and instead trust your application code to do the post request.

Regards



来源:https://stackoverflow.com/questions/12569188/how-to-test-post-request-and-follow-redirects-with-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!