Skip to content

Instantly share code, notes, and snippets.

@arthwood
Created February 19, 2016 15:43
Show Gist options
  • Save arthwood/f580d219ca0fcce17fe0 to your computer and use it in GitHub Desktop.
Save arthwood/f580d219ca0fcce17fe0 to your computer and use it in GitHub Desktop.
Extending routes in tests. How to define extra routing in your tests.
class ApplicationController < ActionController::Base
def logged_in?
session[:user_id].present?
end
def require_login
unless logged_in?
session[:requested_path] = url_for(only_path: true)
redirect_to new_authentication_path
end
end
end
class AuthorizationController < ApplicationController
before_action :require_login
end
Rails.application.routes.tap do |routes|
routes.disable_clear_and_finalize = true
routes.draw do
resources :authorization, only: :index
end
end
describe '#require_login' do
controller AuthorizationController do
def index
render nothing: true
end
end
before do
allow(subject).to receive(:logged_in?).and_return(logged_in)
end
context 'not logged in' do
let(:logged_in) { false }
it 'should be redirect' do
get :index
expect(subject).to have_received(:logged_in?).with(nil)
expect_redirect(response, new_authentication_path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment