Skip to content

Commit 6ce1e07

Browse files
authored
fix: allow current_user and params to remain lazy in rspec contexts (#25)
1 parent 1ed6fd0 commit 6ce1e07

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

lib/graphiti_spec_helpers/rspec.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
require 'rspec/core'
22
require 'graphiti_spec_helpers'
33

4-
::RSpec.shared_context 'resource testing', type: :resource do |parameter|
5-
let(:resource) { described_class }
6-
let(:params) { {} }
4+
class GraphitiContextProxy < OpenStruct
5+
def initialize(proxied, *args)
6+
@__proxied = proxied
7+
super(*args)
8+
end
9+
10+
# variables defined in the rspec context should remain lazy
11+
def current_user
12+
super || __proxied_current_user
13+
end
14+
15+
def params
16+
super || __proxied_params
17+
end
18+
19+
private
20+
21+
def __proxied_current_user
22+
@__proxied.current_user if @__proxied.respond_to?(:current_user)
23+
end
24+
25+
def __proxied_params
26+
@__proxied.params if @__proxied.respond_to?(:params)
27+
end
28+
end
29+
30+
::RSpec.shared_context "resource testing", type: :resource do |parameter|
31+
let(:resource) { described_class }
32+
let(:params) { {} }
733

834
around do |e|
935
begin
@@ -19,12 +45,7 @@
1945
end
2046

2147
def graphiti_context
22-
@graphiti_context ||= begin
23-
ctx = OpenStruct.new
24-
ctx.current_user = current_user if respond_to?(:current_user)
25-
ctx.params = params
26-
ctx
27-
end
48+
@graphiti_context ||= GraphitiContextProxy.new(self)
2849
end
2950

3051
# If you need to set context:

0 commit comments

Comments
 (0)