ActionView::Template::Error: undefined method `children' for nil:NilClas <p>Hello i have an Rspec Test im tring to do for an User controller index action. For user authentication i coded it myself and not using any gems. </p> <p>I get this error when truing to run the test. I looked up all solutions and nothing worked. </p> <pre><code> 2) UsersController GET #index redirects visitor Failure/Error: user_logged_in ActionView::Template::Error: undefined method `children' for nil:NilClass </code></pre> <p>Here is the Test.</p> <pre><code>require "rails_helper" RSpec.describe UsersController, type: :controller do let!(:user) { create(:user) } let!(:users) do users = [] 3.times { users &lt;&lt; create(:user) } users end describe "GET #index" do before do user_logged_in create(users) end it "user renders template and shows users" do get :index expect(response).to render_template(:index) expect(response).to have_http_status(:success) expect(assigns(:users)).to eq(users) end it "redirects visitor" do get :index it { expect(response).to redirect_to(root_path) } end end end </code></pre> <p>Here is rails_helper</p> <pre><code>ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' require "shoulda/matchers" ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.include FactoryGirl::Syntax::Methods config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.include Capybara::DSL end def admin_logged_in visit login_path fill_in 'Email', with: admin.email fill_in 'Password', with: admin.password click_button 'Log In' end def user_logged_in visit login_path fill_in 'Email', with: user.email fill_in 'Password', with: user.password click_button 'Log In' end </code></pre>