Class: RSpec::Core::MemoizedHelpers::ContextHookMemoized

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/core/memoized_helpers.rb

Overview

Used internally to customize the behavior of the memoized hash when used in a `before(:context)` hook.

Direct Known Subclasses

After, Before

Defined Under Namespace

Classes: After, Before

Class Method Summary collapse

Class Method Details

.fetch_or_store(key, &_block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rspec/core/memoized_helpers.rb', line 200

def self.fetch_or_store(key, &_block)
  description = if key == :subject
                  "subject"
                else
                  "let declaration `#{key}`"
                end

  raise <<-EOS
#{description} accessed in #{article} #{hook_expression} hook at:
  #{CallerFilter.first_non_rspec_line}

`let` and `subject` declarations are not intended to be called
in #{article} #{hook_expression} hook, as they exist to define state that
is reset between each example, while #{hook_expression} exists to
#{hook_intention}.
EOS
end

.isolate_for_context_hook(example_group_instance) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rspec/core/memoized_helpers.rb', line 179

def self.isolate_for_context_hook(example_group_instance)
  exploding_memoized = self

  example_group_instance.instance_exec do
    @__memoized = exploding_memoized

    begin
      yield
    ensure
      # This is doing a reset instead of just isolating for context hook.
      # Really, this should set the old @__memoized back into place.
      #
      # Caller is the before and after context hooks
      # which are both called from self.run
      # I didn't look at why it made tests fail, maybe an object was getting reused in RSpec tests,
      # if so, then that probably already works, and its the tests that are wrong.
      __init_memoized
    end
  end
end