Response not reflecting redirection

10 views
Skip to first unread message

Jack R-G

unread,
Jul 29, 2019, 8:01:40 PM7/29/19
to rspec
I have a series of controller tests that have worked in the past. I've just recently tried to resume using RSpec, and I started by trying to get the existing tests working. I'm seeing quite consistently that tests involving controllers are failing, and the problem seems to be with redirection. Below are some tests from one controller and their failure messages. It looks to me like the response is a dummy page with an <a> redirecting to the appropriate page - but I don't know the relevance of that. This controller is the controller that should handle users who are not logged in (that is, it is the entry point for the website).

require 'spec_helper'

describe HomeController do

  render_views

  describe "GET 'index'" do
    describe "when not logged in" do
      before(:each) do
        get 'index'
        test_sign_out
      end

      subject { response }
    
      it { should be_success }
    
      it "should be titled as the app name" do
        should have_selector "title", :content => @app_name
      end
    
      it "should have a header" do
        should have_selector "h1"
      end
    
      describe "login form" do
        it "should be present" do
          should have_selector "form", :action => app_path.sessions_path, :method => "post" do
            it { should have_selector "input", :name => "email", :type => "email" }
            it { should have_selector "input", :name => "password", :type => "password" }        
            it { should have_selector "input", :name => "remember_me", :type => "checkbox" }
            it { should have_selector "input", :name => "commit", :type => "submit", value => "Login" }
            it { should have_selector "a", :content => "Forgot password", :href => app_path.new_password_reset_path }
          end
        end
      end
    
      it "should have a sign up link" do
        should have_selector "a", :content => "Sign up NOW!", :href => app_path.new_user_path
      end
    end
...
  end
end
    

  1) HomeController GET 'index' when not logged in 

     Failure/Error: it { should be_success }

       expected success? to return true, got false

     # ./spec/controllers/home_controller_spec.rb:16:in `block (4 levels) in <top (required)>'


  2) HomeController GET 'index' when not logged in should be titled as the app name

     Failure/Error: should have_selector "title", :content => @app_name

       expected following output to contain a <title/> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="https://www.test.host/">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:19:in `block (4 levels) in <top (required)>'


  3) HomeController GET 'index' when not logged in should have a header

     Failure/Error: should have_selector "h1"

       expected following output to contain a <h1/> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="https://www.test.host/">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:23:in `block (4 levels) in <top (required)>'


  4) HomeController GET 'index' when not logged in should have a sign up link

     Failure/Error: should have_selector "a", :content => "Sign up NOW!", :href => app_path.new_user_path

       expected following output to contain a <a href='/users/new'>Sign up NOW!</a> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="https://www.test.host/">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:39:in `block (4 levels) in <top (required)>'


  5) HomeController GET 'index' when not logged in login form should be present

     Failure/Error: should have_selector "form", :action => app_path.sessions_path, :method => "post" do

       expected following output to contain a <form action='/sessions' method='post'/> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="https://www.test.host/">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:28:in `block (5 levels) in <top (required)>'


Jon Rowe

unread,
Aug 1, 2019, 3:21:32 AM8/1/19
to rs...@googlegroups.com
Hi Jack

From memory and without knowing the specifics, thats a standard “Redirect” page, you might check you’re not trying to send a 400 response and redirect at the same time. You might also like to check that you are following redirects in Capybara.

Cheers
Jon Rowe
---------------------------

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="test.host">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:19:in `block (4 levels) in <top (required)>'

  3) HomeController GET 'index' when not logged in should have a header

     Failure/Error: should have_selector "h1"

       expected following output to contain a <h1/> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="test.host">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:23:in `block (4 levels) in <top (required)>'

  4) HomeController GET 'index' when not logged in should have a sign up link

     Failure/Error: should have_selector "a", :content => "Sign up NOW!", :href => app_path.new_user_path

       expected following output to contain a <a href='/users/new'>Sign up NOW!</a> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="test.host">redirected</a>.</body></html>

     # ./spec/controllers/home_controller_spec.rb:39:in `block (4 levels) in <top (required)>'

  5) HomeController GET 'index' when not logged in login form should be present

     Failure/Error: should have_selector "form", :action => app_path.sessions_path, :method => "post" do

       expected following output to contain a <form action='/sessions' method='post'/> tag:

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "w3.org/TR/REC-html40/loose.dtd">

       <html><body>You are being <a href="test.host">redirected</a>.</body></html>

Jack R-G

unread,
Aug 2, 2019, 2:45:31 AM8/2/19
to rspec
This turned out to not have anything to do with the operation of any of the testing framework. It was due to a code change in my project that invalidated most if not all of the controller tests. I was redirecting requests directed to bare domain names (no "www.") to the canonical ("www.") domain name. Don't ask why - you don't want to know.

Jon Rowe

unread,
Aug 2, 2019, 3:45:20 AM8/2/19
to rs...@googlegroups.com
Hi Jack

Sorry, yes thats what I was trying to say, (its part of rails not the tests). You should check the right status code is being sent during the redirect to prevent that page being seen by users (even momentarily!)

Jon Rowe
---------------------------
Reply all
Reply to author
Forward
0 new messages