From 155e2ad72f1615cfabdedec8fdde8529236259bc Mon Sep 17 00:00:00 2001 From: zvkemp Date: Thu, 7 Mar 2024 10:12:27 -0500 Subject: [PATCH] allow current_user and params to remain lazy in rspec contexts --- lib/graphiti_spec_helpers/rspec.rb | 39 +++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/graphiti_spec_helpers/rspec.rb b/lib/graphiti_spec_helpers/rspec.rb index dccd323..85d158a 100644 --- a/lib/graphiti_spec_helpers/rspec.rb +++ b/lib/graphiti_spec_helpers/rspec.rb @@ -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 @@ -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: