Module: RSpec::Core::Let::ExampleGroupMethods
- Included in:
- SharedContext
- Defined in:
- lib/rspec/core/let.rb
Instance Method Summary collapse
-
#let(name, &block) ⇒ Object
Generates a method whose return value is memoized after the first call.
-
#let!(name, &block) ⇒ Object
Just like
let
, except the block is invoked by an implicitbefore
hook.
Instance Method Details
#let(name, &block) ⇒ Object
let
can enhance readability when used sparingly (1,2, or
maybe 3 declarations) in any given example group, but that can
quickly degrade with overuse. YMMV.
let
uses an ||=
conditional that has the potential to
behave in surprising ways in examples that spawn separate threads,
though we have yet to see this in practice. You've been warned.
Generates a method whose return value is memoized after the first call. Useful for reducing duplication between examples that assign values to the same local variable.
31 32 33 34 35 |
# File 'lib/rspec/core/let.rb', line 31 def let(name, &block) define_method(name) do __memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block) } end end |
#let!(name, &block) ⇒ Object
Just like let
, except the block is invoked by an implicit before
hook. This serves a dual purpose of setting up state and providing a
memoized reference to that state.
90 91 92 93 |
# File 'lib/rspec/core/let.rb', line 90 def let!(name, &block) let(name, &block) before { __send__(name) } end |