Module: RSpec
Overview
The following code inspired and modified from Rails’ assert_response
:
https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/assertions/response.rb#L22-L38
Thank you to all the ::Rails
devs who did the heavy lifting on this!
Constant Summary
-
MODULES_TO_AUTOLOAD =
Internal use only
# File 'rspec-core/lib/rspec/core.rb', line 187{ :Matchers => "rspec/expectations", :Expectations => "rspec/expectations", :Mocks => "rspec/mocks" }
-
Internal use only
# File 'rspec-core/lib/rspec/core/shared_context.rb', line 54Core::SharedContext
Class Attribute Summary
-
.configuration
rw
Returns the global [Configuration](RSpec/Core/Configuration) object.
-
.configuration=(value)
rw
Internal use only
Internal use only
Setters for shared global objects.
-
.current_example
rw
The example being executed.
-
.current_example=(example)
rw
Internal use only
Internal use only
Set the current example being executed.
-
.current_scope ⇒ Symbol
rw
Get the current
RSpec
execution scope. -
.current_scope=(scope)
rw
Internal use only
Internal use only
Set the current scope rspec is executing in.
-
.world
rw
Internal use only
Internal use only
Internal container for global non-configuration data.
-
.world=(value)
rw
Internal use only
Internal use only
Setters for shared global objects.
Class Method Summary
-
.clear_examples
Used to ensure examples get reloaded between multiple runs in the same process and ensures user configuration is persisted.
-
.configure {|Configuration| ... }
Yields the global configuration to a block.
- .const_missing(name) Internal use only Internal use only
-
.reset
Used to ensure examples get reloaded and user configuration gets reset to defaults between multiple runs in the same process.
::RSpec::Support::Warnings
- Extended
deprecate, | |
warn_deprecation | Used internally to print deprecation warnings when rspec-core isn’t loaded. |
warn_with | Used internally to print longer warnings. |
warning | Used internally to print warnings. |
::RSpec::Core::Warnings
- Extended
deprecate | Used internally to print deprecation warnings. |
warn_deprecation | Used internally to print deprecation warnings. |
warn_with |
Class Attribute Details
.configuration (rw)
Returns the global [Configuration](RSpec/Core/Configuration) object. While you can use this method to access the configuration, the more common convention is to use [RSpec.configure](RSpec#configure-class_method).
# File 'rspec-core/lib/rspec/core.rb', line 85
def self.configuration @configuration ||= RSpec::Core::Configuration.new end
.configuration=(value) (rw)
Setters for shared global objects
# File 'rspec-core/lib/rspec/core.rb', line 49
attr_writer :configuration, :world
.current_example (rw)
The example being executed.
The primary audience for this method is library authors who need access to the example currently being executed and also want to support all versions of RSpec
2 and 3.
# File 'rspec-core/lib/rspec/core.rb', line 122
def self.current_example RSpec::Support.thread_local_data[:current_example] end
.current_example=(example) (rw)
Set the current example being executed.
# File 'rspec-core/lib/rspec/core.rb', line 128
def self.current_example=(example) RSpec::Support.thread_local_data[:current_example] = example end
.current_scope ⇒ Symbol
(rw)
Get the current RSpec
execution scope
Returns (in order of lifecycle):
* {:suite} as an initial value, this is outside of the test lifecycle.
* {:before_suite_hook} during `before(:suite)` hooks.
* {:before_context_hook} during `before(:context)` hooks.
* {:before_example_hook} during `before(:example)` hooks and `around(:example)` before {example.run}.
* {:example} within the example run.
* {:after_example_hook} during `after(:example)` hooks and `around(:example)` after {example.run}.
* {:after_context_hook} during `after(:context)` hooks.
* {:after_suite_hook} during `after(:suite)` hooks.
* {:suite} as a final value, again this is outside of the test lifecycle.
Reminder, :context
hooks have :all
alias and :example
hooks have :each
alias.
# File 'rspec-core/lib/rspec/core.rb', line 154
def self.current_scope RSpec::Support.thread_local_data[:current_scope] end
.current_scope=(scope) (rw)
Set the current scope rspec is executing in
# File 'rspec-core/lib/rspec/core.rb', line 134
def self.current_scope=(scope) RSpec::Support.thread_local_data[:current_scope] = scope end
.world (rw)
Internal container for global non-configuration data.
.world=(value) (rw)
Setters for shared global objects
# File 'rspec-core/lib/rspec/core.rb', line 49
attr_writer :configuration, :world
Class Method Details
.clear_examples
Used to ensure examples get reloaded between multiple runs in the same process and ensures user configuration is persisted.
Users must invoke this if they want to clear all examples but preserve current configuration when they use the runner multiple times within the same process.
# File 'rspec-core/lib/rspec/core.rb', line 70
def self.clear_examples world.reset configuration.reset_reporter configuration.start_time = ::RSpec::Core::Time.now configuration.reset_filters end
.configure {|Configuration| ... }
Yields the global configuration to a block.
# File 'rspec-core/lib/rspec/core.rb', line 97
def self.configure yield configuration if block_given? end
.const_missing(name)
# File 'rspec-core/lib/rspec/core.rb', line 194
def self.const_missing(name) # Load rspec-expectations when RSpec::Matchers is referenced. This allows # people to define custom matchers (using `RSpec::Matchers.define`) before # rspec-core has loaded rspec-expectations (since it delays the loading of # it to allow users to configure a different assertion/expectation # framework). `autoload` can't be used since it works with ruby's built-in # require (e.g. for files that are available relative to a load path dir), # but not with rubygems' extended require. # # As of rspec 2.14.1, we no longer require `rspec/mocks` and # `rspec/expectations` when `rspec` is required, so we want # to make them available as an autoload. require MODULES_TO_AUTOLOAD.fetch(name) { return super } ::RSpec.const_get(name) end
.reset
Used to ensure examples get reloaded and user configuration gets reset to defaults between multiple runs in the same process.
Users must invoke this if they want to have the configuration reset when they use the runner multiple times within the same process. Users must deal themselves with re-configuration of RSpec
before run.
# File 'rspec-core/lib/rspec/core.rb', line 58
def self.reset RSpec::ExampleGroups.remove_all_constants @world = nil @configuration = nil end