Assert_select doesn't work after redirect

Can it assert_select

work after assert_redirected_to

? I have the following controller action and I am getting a crash.

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

      

+3


source to share


2 answers


This question is old, but I will go on and answer it since I ran into a similar problem. assert_select

might work after the redirect, but first you have to tell the test to "follow" for the redirect. So, in your case, you can 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

      

+2


source


assert_select

works the same as described in the docs .

I am assuming what you are trying to do assert_select

for some element on the page you are redirecting to. The problem is that the redirected route doesn't actually work.

The first thing I would do if you were you is to throw binding.pry

right before this assert_select

and take a look at response.body

which will show what is actually being displayed. I assume 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>"

      

not the actual page you will be redirected to.

0


source







All Articles