'assert_select not working after redirect

Can an assert_select work after an assert_redirected_to? I have the following controller action and I'm getting a failure.

test "get switchboard" do
  ...
  assert_redirected_to employees_url # success
  assert_select 'div' # fail
end


Solution 1:[1]

This question is old, but I'll go ahead and answer it since I ran into a similar problem. assert_select can work after a redirect, but you must first tell the test to "follow" the redirect. So, in your case, you could do something like this:

test "get switchboard" do
  ...
  assert_redirected_to employees_url # redirected, not "success" per se
  follow_redirect!                   # this is what you need to do
  assert_response :success           # if you still want to...
  assert_select 'div'
end

Solution 2:[2]

assert_select works just as its described in the docs.

I assume that you are trying to do the assert_select on some element on the page that you are redirecting to. The problem with this is that that redirected route is actually not getting rendered.

The first thing that I would do if I were you is toss a binding.pry right in front of that assert_select and take a look at the response.body which will reveal what is actually being rendered. My guess is that you will see something like :

<html><body>You are being <a href=\"https://bigoldtest.net/as/1/oauth_provider_credentials?response_code=success\">redirected</a>.</body></html>"

rather than actual page that you is going to be redirected to.

Solution 3:[3]

The answer is stated above in the comments:

add follow_redirect! after assert_redirected_to and your assert_select statements will pass

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kevinsapp
Solution 2 Dan Bradbury
Solution 3 straightupdev