7070 end
7171
7272 it 'allows access to `super` via a module interceptor' do
73- class BetterString < String
73+ better_string = Class . new ( String ) do
7474 include Cacheable
7575
76- def to_s
76+ cacheable def to_s
7777 "Better #{ super } "
7878 end
79-
80- cacheable :to_s
8179 end
80+ stub_const ( 'BetterString' , better_string )
8281
8382 better_string = BetterString . new ( 'my string' )
8483 expect ( better_string ) . to receive ( :to_s_with_cache ) . twice . and_call_original
@@ -87,18 +86,21 @@ def to_s
8786 end
8887
8988 it 'uses the class name to define an interceptor module' do
90- class RealClassName
91- include Cacheable
92- end
93- class_name = RealClassName
89+ # This is done specifically this way to be compatible w/ RSpec best practices
90+ # Once Cacheable is included in a class, it uses the name of the class to define the
91+ # interceptor module. However, it is considered bad practice to create constants in RSpec
92+ # so they're typically made with `stub_const`. We need to include Cacheable after the
93+ # anonymous class has been created and assigned to the stubbed constant for this order to work.
94+ stub_const ( 'RealClassName' , Class . new )
95+ class_name = RealClassName . include ( described_class )
96+
9497 expect ( class_name . ancestors . map ( &:to_s ) ) . to include ( "Cacheable::#{ class_name } Cacher" )
9598 end
9699
97100 it 'uses the class address to define an interceptor module for anonymous classes' do
98- custom_class = Class . new do
99- include Cacheable
100- end
101+ custom_class = Class . new { include Cacheable }
101102 class_name = custom_class . to_s . tr ( '#:<>' , '' )
103+
102104 expect ( custom_class . ancestors . map ( &:to_s ) ) . to include ( "Cacheable::#{ class_name } Cacher" )
103105 end
104106
@@ -107,9 +109,7 @@ class RealClassName
107109 it "appends #{ special_character } to the end of generated method names" do
108110 method_name = "cacheable_method#{ special_character } "
109111 cacheable_class . class_eval do
110- define_method ( method_name ) { }
111-
112- cacheable method_name
112+ cacheable define_method ( method_name , -> { } )
113113 end
114114
115115 expect ( cacheable_object ) . to respond_to ( "cacheable_method_without_cache#{ special_character } " )
@@ -120,29 +120,10 @@ class RealClassName
120120
121121 context 'when classes are nested' do
122122 it 'correctly names interceptor modules' do
123- expect do
124- class Outer
125- class Inner
126- include Cacheable
123+ stub_const ( 'Outer::Inner' , Class . new )
124+ Outer ::Inner . include ( described_class )
127125
128- cacheable :outer_method
129-
130- def outer_method
131- inner_method
132- 'outer_method'
133- end
134-
135- def inner_method
136- 'inner_method'
137- end
138- end
139- end
140- end . not_to raise_error
141-
142- cacheable_instance = Outer ::Inner . new
143- expect ( cacheable_instance ) . to receive ( :inner_method ) . once . and_call_original
144-
145- 2 . times { cacheable_instance . outer_method }
126+ expect ( Outer ::Inner . ancestors . map ( &:to_s ) ) . to include ( 'Cacheable::OuterInnerCacher' )
146127 end
147128 end
148129 end
@@ -163,9 +144,10 @@ def inner_method
163144 describe 'key generation' do
164145 context 'without any customization' do
165146 it 'uses the class name and method name for the cache key' do
166- TotallyRealClassName = cacheable_class
147+ stub_const ( ' TotallyRealClassName' , cacheable_class )
167148 cacheable_object = TotallyRealClassName . new
168149 key = cacheable_object . cacheable_method_key_format
150+
169151 expect ( key ) . to eq ( [ cacheable_object . class . name , cacheable_method ] )
170152 expect { cacheable_object . cacheable_method }
171153 . to change { described_class . cache_adapter . exist? ( key ) } . from ( false ) . to ( true )
@@ -458,8 +440,10 @@ def cache_control_method
458440 end
459441
460442 it 'uses the class name and method name for the cache key' do
461- AnotherTotallyRealClassName = cacheable_class
462- expect ( AnotherTotallyRealClassName . cacheable_method_key_format ) . to eq ( [ cacheable_class . name , cacheable_method ] )
443+ stub_const ( 'AnotherTotallyRealClassName' , cacheable_class )
444+ key = AnotherTotallyRealClassName . cacheable_method_key_format
445+
446+ expect ( key ) . to eq ( [ cacheable_class . name , cacheable_method ] )
463447 end
464448 end
465449
0 commit comments