Testing Rails Controller with Session -
Testing Rails Controller with Session -
in app i've got session controller. want test users controller accessible if 1 logged in.
my problem how can false session. i've tried lot hope can help me now:
the application controller:
helper_method :current_user def authenticate_user current_user if @current_user homecoming true else redirect_to(:controller => 'sessions', :action => 'new') homecoming false end end private def current_user @current_user ||= user.find(session[:user_id]) if session[:user_id] end
the users controller has:
before_filter :authenticate_user
the controller test:
describe "get index" "assigns users @users" user = factorygirl.create(:user) @request.session[:user_id] = user.id 'users' expect(@users.size).to eq(1) end end
i've tried session[:user_id] = user.id
, debugging (after request):
@current_user
nil user.find(session[:user_id])
returns right user
try controller
instead of @request
:
user = factorygirl.create(:user) controller.session[:user_id] = user.id 'users' expect(@users.size).to eq(1)
also have @users
variable defined in test? should expect(user.count).to eq(1)
session testing ruby-on-rails-4 rspec controller
Comments
Post a Comment