I am not able to access session or route paths (root_url) from the specs using anonymous controller on a controller concern.
Here is my code
module SecuredConcern
extend ActiveSupport::Concern
def logged_in?
session[:user_info].present?
end
def redirect_to_login
redirect_to login_path unless logged_in?
end
def redirect_to_home
redirect_to root_path if logged_in?
end
end
And spec
require 'rails_helper'
describe SecuredConcern, type: :controller do
before do
class FakesController < ApplicationController
include SecuredConcern
end
end
after { Object.send :remove_const, :FakesController }
let(:object) { FakesController.new }
# let(:session) {create(:session, user_info: '')}
describe 'logged_in' do
it "should return false if user is not logged in" do
expect(object.logged_in?).to eq(false)
end
end
end
Here is the trace:
Module::DelegationError:
ActionController::Metal#session delegated to @_request.session, but @_request is nil: #<FakesController:0x00007f9856c04c20 @_action_has_layout=true, @rendered_format=nil, @_routes=nil, @_request=nil, @_response=nil>
# ./app/controllers/concerns/secured_concern.rb:9:in `logged_in?'
# ./spec/controllers/concerns/secured_concern_spec.rb:21:in `block (3 levels) in <main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `session' for nil:NilClass
# ./app/controllers/concerns/secured_concern.rb:9:in `logged_in?'
config is updated with config.infer_base_class_for_anonymous_controllers = true
Any pointers on what I am doing wrong here?