RSpec InfiniteRedirectError when visiting root_path

This is with the capybara test function. I don't have before_action filters set in my controller for the root page, so I'm completely stumped by this. Has anyone else had the same problem?

The line causing the error is simply

visit(root_path) 

      

Very strange.

Also, when running this test, it itself passes, but when running the entire test suite, it fails with an InfiniteRedirectError.

user_and_role_spec.rb:

require 'rails_helper'

    def manually_create_user
        visit new_user_registration_path
        fill_in('user_first_name', :with => 'Test')
        fill_in('user_last_name', :with => 'User')
        fill_in('user_email', :with => 'testuser@email.com')
        fill_in('user_password', :with => 'testuser')
        fill_in('user_password_confirmation', :with => 'testuser')
        click_button('Sign up')
    end

    def create_user_and_login_as(type)
        user = FactoryGirl.create(type)
        visit(new_user_session_path)
        fill_in('user_email', :with => user.email)
        fill_in('user_password', :with => user.password)
        click_button('Log in')
    end


describe 'with users and roles' do  

    context "if user is not an admin" do

        it "makes sure Login/Logout works" do
            visit(root_path)
            click_link("Sign up")
            fill_in('user_email', :with => "testuser@email.com")
            fill_in('user_first_name', :with => "Test")
            fill_in('user_last_name', :with => "User")
            fill_in('user_password', :with => "password")
            fill_in('user_password_confirmation', :with => "password")
            click_button "Sign up"
            expect(current_path).to eq(root_path)
            expect(page).to have_content('Welcome! You have signed up successfully.')
        end
    end
end

      

static_pages_controller.rb:

class StaticPagesController < ApplicationController
    before_action :an_admin?, only: [:admin]

  def home
    @testimonials = Testimonial.all
  end

  def admin
    @groups = Group.all

    @users = User.all

    @students = Student.all

    @teachers = Teacher.all
  end

  private

  def an_admin?
    unless signed_in? && (current_user.admin == true) 
      redirect_to root_path, notice: "You have to be a signed-in admin to view the admin page"
    end
  end

end

      

routes.rb:

Rails.application.routes.draw do

  resources :materials

  root 'static_pages#home'

  devise_for :users, :controllers => { registrations: 'registrations' }

  get 'admin' => 'static_pages#admin'

  resources :groups
  resources :users
  resources :students
  resources :teachers
  resources :testimonials
  post 'assign_to_group' => 'students#assign_to_group' # Could have been 'patch', but default in the controller method is 'post', so I left the method as default and changed this route to 'post'. Doesn't NEED to be patch.
  post 'remove_from_group' => 'students#remove_from_group'
  post 'unassign_teacher' => 'groups#unassign_teacher'
  post 'assign_as_student' => 'teachers#assign_as_student'
  post 'assign_as_teacher' => 'students#assign_as_teacher'
  post 'add_student' => 'groups#add_student'
  post 'remove_student_from_group' => 'groups#remove_student_from_group'
end

      

rails_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'database_cleaner'

require 'devise'

# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  config.include Devise::TestHelpers, :type => :controller

  Capybara.register_driver :rack_test do |app|
    Capybara::RackTest::Driver.new(app, :respect_data_method => true, :redirect_limit => 20)
  end

end

      

spec_helper.rb:

RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end
end

      

test.log:

Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected
Completed 302 Found in 6ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected
Completed 302 Found in 7ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected

      

... etc. etc.

For me, it says my before_action in my static_pages_controller kicks in, but I don't understand why that would be, given an_admin? method code?

UPDATE:

I can get closer to solving this question: static_controller_spec runs before user_and_role_spec when running the full test suite. When I disable static_controller_spec, the user_and_role_spec test runs without error when running the entire test suite. Seems to be the culprit for this line:

controller.class.skip_before_action :an_admin?

      

This line is in static_controller_spec:

require "rails_helper.rb"

describe StaticPagesController do



describe "GET #home" do
        it "renders the :home view" do
            get :home
            expect(response).to render_template :home
        end
    end

    describe "GET #admin" do
        it "renders the :admin view" do
            # This is line 14. The next line is intended to disable the :an_admin? before_action in the controller
            controller.class.skip_before_action :an_admin?
            get :admin 
            expect(response).to render_template :admin
            # The next line is intended to reverse line 15
            controller.class.before_action :an_admin?
        end

        it "requires user to be signed_in"

        it "requires user to be an admin"
    end

end

      

I wanted to disable before_action for this test. To be honest, I don't really understand this line - it was a direct copy / paste from somewhere. However, it looks like I'm trying to execute my user_and_role_spec test, but I don't understand why. Any ideas?

+3


source to share


2 answers


In your user_and_role_spec.rb, you have an additional one end

after the close is complete def create_user_and_login_as(type)

. You need the two end

at the bottom of the spec to close describe 'with users and roles' do

and context "if user is not an admin" do

.

In your route.rb you need end

at the bottom to closeRails.application.routes.draw do



I'm not sure if you just didn't insert them, but it does matter in your application, and it does matter when you post code.

0


source


DECIDE:

As for the update in my original post, I just completely removed those two controller.class lines in static_pages_controller_spec and the whole test suite passes (thank goodness!). Of course, it makes me wonder why I had these lines there in the first place - I just can't remember.



However, this is all a bit of a mystery, so I started this other thread to try and shed some light on it. I know I am asking for a little manual work here, but any help would be much appreciated. I am doing a lot too, but it is also very helpful to introduce other people as well.
0


source







All Articles