DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on

6 3 1 1

follow_redirect! in Rails request specs

In integration tests we test the complete scenario of a user interaction. Typically it means, we hit different URLs of the application. In such situations, it may happen that one controller action redirect to another action in same or different controller and we need to visit that page to test the further part of the scenario.

Typical example will be user signing up on our website. After signing up the user must be shown welcome screen. In an integration test, we submit the sign up form and then we need to test whether user sees welcome text or not.

  post "/join", params: user_params
  expect(response).to redirect_to("/welcome")
  # Test that welcome page displays certain content
Enter fullscreen mode Exit fullscreen mode

Rails provides a handy method follow_redirect! for this purpose. As the name suggests, it follows a redirect response.

  post "/join", params: user_params
  expect(response).to redirect_to("/welcome")

  follow_redirect!

  # get "/welcome" is automatically hit with current user as the new 
  # user
Enter fullscreen mode Exit fullscreen mode

In RSpec, we can use this method in the request specs. If you are using minitest with Rails then you can use this method in integration tests.
This method is very handy and helps in testing a complex scenario involving multiple steps a breeze.

Happy testing!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay