Created
December 4, 2014 01:09
-
-
Save jeffdean-galvanize/821e99ccf7d7b23fd462 to your computer and use it in GitHub Desktop.
Spec before_actions in RSpec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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