Rails Kiddie Rails Kiddie -4 years ago 100
Ruby Question

Why is Rails not giving a 3XX on redirect and causing my unit tests to fail?

I'm trying out some unit tests for the first time in Ruby on Rails 5.0.1 (designed sites a few times without tests but just starting to understand the value of proper testing...) and writing a sample app to get the hang of things. I have written some tests to make sure the routing works correctly.

test/controllers/static_page_controllers_test

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "root should get static_pages home" do
get '/'
assert_redirected_to static_pages_home_url
end

test "static_pages root should get static_pages home" do
get '/static_pages'
assert_redirected_to static_pages_home_url
end

[some other tests]

end


I have written some routing code to redirect from root and
static_pages
root to the
static_pages
home
embedded ruby html file. This code does correctly redirect in the sense that when you point the browser at
/static_pages
or
/
the
static_pages/home.html.erb
page loads.

app/config/routes.rb

Rails.application.routes.draw do
get '/static_pages' => 'static_pages#home'
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
root 'static_pages#home'
end


When I run my unit tests for redirects they are still failing.

rails test


# Running:

F

Failure:
StaticPagesControllerTest#test_root_should_get_static_pages_home [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:6]:
Expected response to be a <3XX: redirect>, but was a <200: OK>


bin/rails test test/controllers/static_pages_controller_test.rb:4

..F

Failure:
StaticPagesControllerTest#test_static_pages_root_should_get_static_pages_home [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:11]:
Expected response to be a <3XX: redirect>, but was a <200: OK>


bin/rails test test/controllers/static_pages_controller_test.rb:9

.

Finished in 0.821392s, 6.0872 runs/s, 6.0872 assertions/s.

5 runs, 5 assertions, 2 failures, 0 errors, 0 skips


A quick
wget
to the root URL confirms the Rails Puma server is returning HTTP 200 instead of a 300. Do I need to manually alter the HTTP requests themselves or am I handling the redirect wrongly in some manner? How best to change this so my unit tests work correctly and the server returns the right HTTP status code?

Answer Source

The problem is that these are not actual HTTP redirects. When you set up your root within routes.rb, and then visit "/", you are not going to some page first and then being told via some code that you should be redirected to a different page - you're just being taken straight to the root/home page.

Same goes for when you set up "/static_pages" to map (not redirect) to "static_pages#home". What is happening is that the Rails server knows that when someone goes to "/static_pages", the StaticPagesController is going to respond, specifically, the #home method. There is no redirect happening.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download