Last active
August 14, 2018 15:42
-
-
Save jc00ke/5d05894deb9be28cb5d5f7b92303e343 to your computer and use it in GitHub Desktop.
Using Module prepend with an argument
This file contains 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
# spec/support/authenticated.rb | |
class Authenticated < Module | |
def initialize(current_user:) | |
super() do | |
define_method :current_user do | |
current_user | |
end | |
end | |
end | |
end | |
RSpec.shared_context "authenticated", shared_context: :metadata do | |
before do | |
described_class.prepend Authenticated.new(current_user: current_user) | |
end | |
end |
This file contains 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
# 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
Thanks to @epidemian and his suggestion to mock the described class:
I find this to be much more straightforward and clear. I think I tried something akin to this but for some reason it didn't work. Not sure why. Anyway, this is very nice indeed!