Skip to content

Instantly share code, notes, and snippets.

@jeffdean-galvanize
Created December 4, 2014 01:09
Show Gist options
  • Select an option

  • Save jeffdean-galvanize/821e99ccf7d7b23fd462 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdean-galvanize/821e99ccf7d7b23fd462 to your computer and use it in GitHub Desktop.
Spec before_actions in RSpec
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def current_user
User.find_by(id: session[:user_id])
end
helper_method :current_user
def ensure_logged_in_user
unless current_user
redirect_to signin_path
end
end
before_action :ensure_logged_in_user
end
require 'rails_helper'
describe ApplicationController do
describe "permissions" do
controller do
def index
render nothing: true
end
end
it "redirects non-logged in users to signin" do
get :index
expect(response).to redirect_to(signin_path)
end
it "allows logged-in users to see pages" do
session[:user_id] = create_user.id
get :index
expect(response).to be_success
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment