Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow current_user and params to remain lazy in rspec contexts #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/graphiti_spec_helpers/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
require 'graphiti_spec_helpers'

::RSpec.shared_context 'resource testing', type: :resource do |parameter|
let(:resource) { described_class }
let(:params) { {} }
class GraphitiContextProxy < OpenStruct
def initialize(proxied, *args)
@__proxied = proxied
super(*args)
end

# variables defined in the rspec context should remain lazy
def current_user
super || __proxied_current_user
end

def params
super || __proxied_params
end

private

def __proxied_current_user
@__proxied.current_user if @__proxied.respond_to?(:current_user)
end

def __proxied_params
@__proxied.params if @__proxied.respond_to?(:params)
end
end

::RSpec.shared_context "resource testing", type: :resource do |parameter|
let(:resource) { described_class }
let(:params) { {} }

around do |e|
begin
Expand All @@ -18,12 +44,7 @@
end

def graphiti_context
@graphiti_context ||= begin
ctx = OpenStruct.new
ctx.current_user = current_user if respond_to?(:current_user)
ctx.params = params
ctx
end
@graphiti_context ||= GraphitiContextProxy.new(self)
end

# If you need to set context:
Expand Down