Module: RSpec::Core::MemoizedHelpers::ClassMethods
- Included in:
- ExampleGroup
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/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
-
#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 implicit `before` hook.
-
#subject(name = nil, &block) ⇒ Object
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.
-
#subject!(name = nil, &block) ⇒ Object
Just like ‘subject`, except the block is invoked by an implicit `before` 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` can be configured to be threadsafe or not. If it is threadsafe, it will take longer to access the value. If it is not threadsafe, it may behave in surprising ways in examples that spawn separate threads. Specify this on `RSpec.configure`
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.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/memoized_helpers.rb', line 306 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? # A list of reserved words that can't be used as a name for a memoized helper # Matches for both symbols and passed strings if [:initialize, :to_s].include?(name.to_sym) raise ArgumentError, "#let or #subject called with reserved name `#{name}`" end our_module = MemoizedHelpers.module_for(self) # If we have a module clash in our helper module # then we need to remove it to prevent a warning. # # Note we do not check ancestor modules (see: `instance_methods(false)`) # as we can override them. if our_module.instance_methods(false).include?(name) our_module.__send__(:remove_method, name) end our_module.__send__(:define_method, name, &block) # If we have a module clash in the example module # then we need to remove it to prevent a warning. # # Note we do not check ancestor modules (see: `instance_methods(false)`) # as we can override them. if instance_methods(false).include?(name) remove_method(name) end # 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_or_store(name) { super(RSpec.current_example, &nil) } } else define_method(name) { __memoized.fetch_or_store(name) { super(&nil) } } 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.
400 401 402 403 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/memoized_helpers.rb', line 400 def let!(name, &block) let(name, &block) before { __send__(name) } end |
#subject(name = nil, &block) ⇒ Object
‘subject` can be configured to be threadsafe or not. If it is threadsafe, it will take longer to access the value. If it is not threadsafe, it may behave in surprising ways in examples that spawn separate threads. Specify this on `RSpec.configure`
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.
444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/memoized_helpers.rb', line 444 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 |
#subject!(name = nil, &block) ⇒ Object
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.
510 511 512 513 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-core-3.12.0/lib/rspec/core/memoized_helpers.rb', line 510 def subject!(name=nil, &block) subject(name, &block) before { subject } end |