Module: RSpec::Core::MemoizedHelpers::ClassMethods
- Included in:
- ExampleGroup
- Defined in:
- lib/rspec/core/memoized_helpers.rb
Overview
This module is extended onto ExampleGroup, making the methods available to be called from within example group blocks. You can think of them as being analagous to class macros.
Instance Method Summary (collapse)
-
- (void) let(name, &block)
Generates a method whose return value is memoized after the first call.
-
- (void) let!(name, &block)
Just like
let
, except the block is invoked by an implicitbefore
hook. -
- (void) subject(name = nil, &block)
Declares a
subject
for an example group which can then be wrapped withexpect
usingis_expected
to make it the target of an expectation in a concise, one-line example. -
- (void) subject!(name = nil, &block)
Just like
subject
, except the block is invoked by an implicitbefore
hook.
Instance Method Details
- (void) let(name, &block)
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.
Because let
is designed to create state that is reset between
each example, and before(:context)
is designed to setup state that
is shared across all examples in an example group, let
is not
intended to be used in a before(:context)
hook.
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.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rspec/core/memoized_helpers.rb', line 231 def let(name, &block) # We have to pass the block directly to `define_method` to # allow it to use method constructs like `super` and `return`. raise "#let or #subject called without a block" if block.nil? MemoizedHelpers.module_for(self).__send__(:define_method, name, &block) # Apply the memoization. The method has been defined in an ancestor # module so we can use `super` here to get the value. if block.arity == 1 define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(RSpec.current_example, &nil) } } else define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(&nil) } } end end |
- (void) let!(name, &block)
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.
299 300 301 302 |
# File 'lib/rspec/core/memoized_helpers.rb', line 299 def let!(name, &block) let(name, &block) before { __send__(name) } end |
- (void) subject(name = nil, &block)
Declares a subject
for an example group which can then be wrapped
with expect
using is_expected
to make it the target of an
expectation in a concise, one-line example.
Given a name
, defines a method with that name which returns the
subject
. This lets you declare the subject once and access it
implicitly in one-liners and explicitly using an intention revealing
name.
When given a name
, calling super
in the block is not supported.
338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/rspec/core/memoized_helpers.rb', line 338 def subject(name=nil, &block) if name let(name, &block) alias_method :subject, name self::NamedSubjectPreventSuper.__send__(:define_method, name) do raise NotImplementedError, "`super` in named subjects is not supported" end else let(:subject, &block) end end |
- (void) subject!(name = nil, &block)
Just like subject
, 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.
404 405 406 407 |
# File 'lib/rspec/core/memoized_helpers.rb', line 404 def subject!(name=nil, &block) subject(name, &block) before { subject } end |