Skip to content

Instantly share code, notes, and snippets.

@epidemian
Forked from jc00ke/authenticated.rb
Last active August 14, 2018 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epidemian/d1e629b4ba9492e1250403bd324b313a to your computer and use it in GitHub Desktop.
Save epidemian/d1e629b4ba9492e1250403bd324b313a to your computer and use it in GitHub Desktop.
Using RSpec mocks instead of a dynamic module and module prepend
# spec/support/authenticated.rb
RSpec.shared_context "authenticated", shared_context: :metadata do
before do
allow_any_instance_of(described_class).to receive(:current_user) { current_uset }
end
end
# spec/admin/controllers/study/create_spec.rb
RSpec.describe Admin::Controllers::Study::Create, type: :action do
let(:action) { described_class.new }
let(:params) { Hash[] }
let(:admin) { Fabricate.build(:admin) }
include_context "authenticated" do
let(:current_user) { admin }
end
it 'is successful' do
response = action.call(params)
expect(response[0]).to eq 302
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment