Class: RSpec::Core::Configuration
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Exceptions:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Module.new,
Hooks
|
|
Inherits: | Object |
Defined in: | rspec-core/lib/rspec/core/configuration.rb |
Overview
Stores runtime configuration information.
Configuration
options are loaded from multiple files and joined together with command-line switches and the SPEC_OPTS
environment variable.
Precedence order (where later entries overwrite earlier entries on conflicts):
* Global (`$XDG_CONFIG_HOME/rspec/options`, or {~/.rspec} if it does
not exist)
* Project-specific (`./.rspec`)
* Local (`./.rspec-local`)
* Command-line options
* {SPEC_OPTS}
For example, an option set in the local file will override an option set in your global file.
The global, project-specific and local files can all be overridden with a separate custom file using the –options command-line parameter.
Constant Summary
-
DEFAULT_FORMATTER =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1658lambda { |string| string }
-
FAILED_STATUS =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1133"failed".freeze
-
MOCKING_ADAPTERS =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 756{ :rspec => :RSpec, :flexmock => :Flexmock, :rr => :RR, :mocha => :Mocha, :nothing => :Null }
-
PASSED_STATUS =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1136"passed".freeze
-
PENDING_STATUS =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1139"pending".freeze
-
RAISE_ERROR_WARNING_NOTIFIER =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1777lambda { || raise }
-
Readers =
Internal use only
Module that holds
attr_reader
declarations. It’s in a separate module to allow us to override those methods and usesuper
.Module.new
-
UNKNOWN_STATUS =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1130"unknown".freeze
-
VALID_STATUSES =
Internal use only
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1142[UNKNOWN_STATUS, FAILED_STATUS, PASSED_STATUS, PENDING_STATUS]
Class Method Summary
-
.add_read_only_setting(name, opts = {})
Internal use only
Internal use only
As .add_setting but only add the reader.
-
.add_setting(name, opts = {})
Internal use only
Internal use only
Invoked by the .add_setting instance method.
- .define_alias(name, alias_name) Internal use only Internal use only
- .define_predicate(name) Internal use only Internal use only
- .define_reader(name) Internal use only Internal use only
- .delegate_to_ordering_manager(*methods) Internal use only Internal use only
-
.new ⇒ Configuration
constructor
Build an object to store runtime configuration options and set defaults.
Instance Attribute Summary
-
#backtrace_exclusion_patterns ⇒ Array<Regexp>
rw
Regexps used to exclude lines from backtraces.
-
#backtrace_exclusion_patterns=(patterns)
rw
Set
regular expressions used to exclude lines in backtrace. - #backtrace_formatter readonly Internal use only Internal use only
-
#backtrace_inclusion_patterns ⇒ Array<Regexp>
rw
Regexps used to include lines in backtraces.
-
#backtrace_inclusion_patterns=(patterns)
rw
Set
regular expressions used to include lines in backtrace. -
#bisect_runner ⇒ Symbol
rw
Determines which bisect runner implementation gets used to run subsets of the suite during a bisection.
- #bisect_runner=(value) rw
-
#color ⇒ Boolean
rw
deprecated
Deprecated.
No longer recommended because of complex behavior. Instead, rely on the fact that TTYs will display color by default, or set #color_mode to
:on
to display color on a non-TTY output. -
#color=(value)
rw
deprecated
Deprecated.
No longer recommended because of complex behavior. Instead, rely on the fact that TTYs will display color by default, or set
:color_mode
to:on
to display color on a non-TTY output. -
#color_mode ⇒ Boolean
rw
The mode for determining whether to display output in color.
-
#color_mode=(value)
rw
Set
the color mode. -
#default_color ⇒ Symbol
rw
The default output color.
-
#default_formatter
rw
The formatter that will be used if no formatter has been set.
-
#default_formatter=(value)
rw
Sets a fallback formatter to use if none other has been set.
-
#default_path ⇒ String
rw
Path to use if no path is provided to the
rspec
command (default:“spec”
). -
#default_path=(path) ⇒ String
rw
Path to use if no path is provided to the
rspec
command (default:“spec”
). -
#deprecation_stream ⇒ IO, String
rw
Determines where deprecation warnings are printed.
-
#deprecation_stream=(value)
rw
Determines where deprecation warnings are printed.
-
#detail_color ⇒ Symbol
rw
Color used to print details.
- #disable_monkey_patching rw Internal use only Internal use only
-
#drb ⇒ Boolean
rw
Run examples over DRb (default:
false
). -
#drb_port
rw
The drb_port (default: nil).
-
#dry_run
rw
Prints the formatter output of your suite without running any examples or hooks.
-
#error_exit_code ⇒ Integer
rw
The exit code to return if there are any errors outside examples (default: failure_exit_code).
-
#error_stream
rw
Default:
$stderr
. -
#example_status_persistence_file_path ⇒ String
rw
The file path to use for persisting example statuses.
-
#example_status_persistence_file_path=(value)
rw
Sets the file path to use for persisting example statuses.
-
#exclude_pattern ⇒ String
rw
Exclude files matching this pattern.
-
#exclude_pattern=(value)
rw
Set
pattern to match files to exclude. -
#exclusion_filter
rw
Returns the #exclusion_filter.
-
#exclusion_filter=(filter)
rw
Clears and reassigns the #exclusion_filter.
-
#expectation_framework=(framework)
writeonly
Delegates to expect_with(framework).
-
#expose_dsl_globally=(value)
rw
Use this to expose the core
::RSpec
DSL viaModule
and themain
object. -
#expose_dsl_globally? ⇒ Boolean
rw
Indicates if the
DSL
has been exposed off of modules andmain
. -
#fail_fast
rw
If specified, indicates the number of failures required before cleaning up and exit (default:
nil
). - #fail_fast=(value) rw
-
#fail_if_no_examples ⇒ Boolean
rw
Whether or not to fail when there are no
::RSpec
examples (default: false). -
#failure_color ⇒ Symbol
rw
Color to use to indicate failure.
-
#failure_exit_code ⇒ Integer
rw
The exit code to return if there are any failures (default: 1).
-
#files_to_run ⇒ Array
rw
The spec files
::RSpec
will run. - #files_to_run=(value) rw Internal use only Internal use only
-
#filter
rw
Alias for #inclusion_filter.
- #filter_manager rw Internal use only Internal use only
-
#fixed_color ⇒ Symbol
rw
Color used when a pending example is fixed.
-
#full_backtrace=(true_or_false)
rw
Toggle full backtrace.
-
#full_backtrace? ⇒ Boolean
rw
Check if full backtrace is enabled.
- #full_description ⇒ Array rw
-
#full_description=(description)
rw
Run examples matching on
description
in all files to run. -
#inclusion_filter
(also: #filter)
rw
Returns the #inclusion_filter.
-
#inclusion_filter=(filter)
(also: #filter=)
rw
Clears and reassigns the #inclusion_filter.
-
#libs ⇒ Array<String>
rw
Returns dirs that have been prepended to the load path by the
-I
command line option. - #libs=(libs) rw Internal use only Internal use only
- #loaded_spec_files readonly Internal use only Internal use only
-
#max_displayed_failure_line_count
rw
Maximum count of failed source lines to display in the failure reports (defaults to
10
). -
#mock_framework ⇒ Symbol
rw
Returns the configured mock framework adapter module.
-
#mock_framework=(framework)
rw
Delegates to mock_framework=(framework).
-
#only_failures?
readonly
Alias for #only_failures.
- #only_failures_but_not_configured? ⇒ Boolean readonly Internal use only Internal use only
- #ordering_manager readonly Internal use only Internal use only
-
#output_stream ⇒ IO, String
rw
Determines where
::RSpec
will send its output. -
#output_stream=(value)
rw
Set
the output stream for reporter. -
#pattern ⇒ String
rw
Load files matching this pattern (default: ‘’**
,//*
/*_spec.rb’‘). -
#pattern=(value)
rw
Set
pattern to match files to load. -
#pending_color ⇒ Symbol
rw
Color to use to print pending examples.
-
#pending_failure_output
rw
Format the output for pending examples.
-
#pending_failure_output=(mode)
rw
Format the output for pending examples.
-
#profile_examples ⇒ Boolean
rw
Report the times for the slowest examples (default:
false
). -
#profile_examples=(value) ⇒ Boolean
rw
Report the times for the slowest examples (default:
false
). -
#project_source_dirs ⇒ Array<String>
rw
Specifies which directories contain the source code for your project.
-
#raise_on_warning=(value)
writeonly
Turns RSpec warnings into errors.
-
#requires ⇒ Array<String>
rw
Indicates files configured to be required.
- #requires=(paths) rw Internal use only Internal use only
-
#run_all_when_everything_filtered
rw
deprecated
Deprecated.
Use #filter_run_when_matching instead for the specific filters that you want to be ignored if none match.
-
#shared_context_metadata_behavior ⇒ :trigger_inclusion, :apply_to_host_groups
rw
Configures how
::RSpec
treats metadata passed as part of a shared example group definition. - #shared_context_metadata_behavior=(value) rw
-
#silence_filter_announcements
rw
Don’t print filter info i.e. “Run options: include
:focus=>true
” (defaultfalse
). - #static_config_filter_manager rw Internal use only Internal use only
-
#success_color ⇒ Symbol
rw
Color to use to indicate success.
-
#threadsafe
rw
Use threadsafe options where available.
- #treat_symbols_as_metadata_keys_with_true_values=(_value) writeonly deprecated Deprecated.
-
#warnings=(value)
rw
Set
Ruby warnings on or off. - #warnings? ⇒ Boolean rw
- #world rw Internal use only Internal use only
- #pattern_might_load_specs_from_vendored_dirs? ⇒ Boolean readonly private
- #rspec_expectations_loaded? ⇒ Boolean readonly private
- #rspec_mocks_loaded? ⇒ Boolean readonly private
Instance Method Summary
-
#add_formatter(formatter)
(also: #formatter=)
Adds a formatter to the set
::RSpec
will use for this run. -
#add_setting(name)
Adds a custom setting to the
::RSpec
.configuration object. -
#after(scope = nil, *meta, &block)
(also: #prepend_after)
Defines a #after hook.
-
#alias_example_group_to(new_name, *args)
Creates a method that defines an example group with the provided metadata.
-
#alias_example_to(name, *args)
Creates a method that delegates to
example
including the submittedargs
. -
#alias_it_behaves_like_to(new_name, report_label = '')
(also: #alias_it_should_behave_like_to)
Define an alias for it_should_behave_like that allows different language (like “it_has_behavior” or “it_behaves_like”) to be employed when including shared examples.
-
#alias_it_should_behave_like_to(new_name, report_label = '')
Alias for #alias_it_behaves_like_to.
- #append_after(scope = nil, *meta, &block)
-
#append_before(scope = nil, *meta, &block)
Alias for #before.
- #apply_derived_metadata_to(metadata) Internal use only Internal use only
-
#around(scope = nil, *meta, &block)
Registers
block
as an #around hook. -
#before(scope = nil, *meta, &block)
(also: #append_before)
Defines a #before hook.
- #bisect_runner_class Internal use only Internal use only
-
#color_enabled?(output = output_stream) ⇒ Boolean
Check if color is enabled for a particular output.
- #configure_example(example, example_hooks) Internal use only Internal use only
- #configure_expectation_framework Internal use only Internal use only
- #configure_group(group) Internal use only Internal use only
- #configure_mock_framework Internal use only Internal use only
-
#define_derived_metadata(*filters) {|metadata| ... }
Defines a callback that can assign derived metadata values.
-
#disable_monkey_patching!
Enables zero monkey patching mode for
::RSpec
. -
#expect_with(*frameworks)
Sets the expectation framework module(s) to be included in each example group.
-
#expectation_frameworks
Returns the configured expectation framework adapter module(s).
-
#expose_current_running_example_as(method_name)
Exposes the current running example via the named helper method.
-
#extend(mod, *filters)
Tells RSpec to extend example groups with
mod
. - #files_or_directories_to_run=(*files) Internal use only Internal use only
-
#filter_gems_from_backtrace(*gem_names)
Adds #backtrace_exclusion_patterns that will filter lines from the named gems from backtraces.
-
#filter_run(*args)
Alias for #filter_run_including.
-
#filter_run_excluding(*args)
Adds key/value pairs to the #exclusion_filter.
-
#filter_run_including(*args)
(also: #filter_run)
Adds key/value pairs to the #inclusion_filter.
-
#filter_run_when_matching(*args)
Applies the provided filter only if any of examples match, in constrast to #filter_run, which always applies even if no examples match, in which case no examples will be run.
-
#force(hash)
Internal use only
Internal use only
Used to set higher priority option values from the command line.
-
#format_docstrings(&block)
Formats the docstring output using the block provided.
- #format_docstrings_block Internal use only Internal use only
-
#formatter=(formatter, output = output_wrapper)
Alias for #add_formatter.
- #formatter_loader Internal use only Internal use only
-
#formatters ⇒ Array
Returns a duplicate of the formatters currently loaded in the
FormatterLoader
for introspection. -
#hooks
Internal use only
Internal use only
Holds the various registered hooks.
- #in_project_source_dir_regex Internal use only Internal use only
-
#include(mod, *filters)
Tells RSpec to include
mod
in example groups. -
#include_context(shared_group_name, *filters)
Tells RSpec to include the named shared example group in example groups.
- #last_run_statuses Internal use only Internal use only
- #load_spec_files Internal use only Internal use only
-
#mock_with(framework)
Sets the mock framework adapter module.
-
#on_example_group_definition(&block)
Invokes block before defining an example group.
-
#on_example_group_definition_callbacks
Internal use only
Internal use only
Returns an array of blocks to call before defining an example group.
-
#only_failures
(also: #only_failures?)
readonly
Indicates if the
–only-failures
(or–next-failure
) flag is being used. -
#order=(value)
Sets the default global ordering strategy.
-
#prepend(mod, *filters)
Tells RSpec to prepend example groups with
mod
. -
#prepend_after(scope = nil, *meta, &block)
Alias for #after.
- #prepend_before(scope = nil, *meta, &block)
-
#raise_errors_for_deprecations!
Turns deprecation warnings into errors, in order to surface the full backtrace of the call site.
-
#register_ordering(name) {|list| ... }
Registers a named ordering strategy that can later be used to order an example group’s subgroups by adding ‘:order => <name>` metadata to the example group.
- #reporter ⇒ RSpec::Core::Reporter
- #reset Internal use only Internal use only
- #reset_filters Internal use only Internal use only
- #reset_reporter Internal use only Internal use only
-
#seed
Seed for random ordering (default: generated randomly each run).
-
#seed=(value)
Sets the seed value and sets the default global ordering to random.
- #spec_files_with_failures Internal use only Internal use only
-
#when_first_matching_example_defined(*filters)
Defines a callback that runs after the first example with matching metadata is defined.
- #with_suite_hooks Internal use only Internal use only
-
#absolute_pattern?(pattern) ⇒ Boolean
private
:nocov:
- #add_hook_to_existing_matching_groups(meta, scope, &block) private
- #assert_no_example_groups_defined(config_option) private
- #clear_values_derived_from_example_status_persistence_file_path private
- #command private
- #conditionally_disable_expectations_monkey_patching private
- #conditionally_disable_mocks_monkey_patching private
- #configure_group_with(group, module_list, application_method) private
- #define_built_in_hooks private
- #define_mixed_in_module(mod, filters, mod_list, config_method, &block) private
- #extract_location(path) private
- #file_glob_from(path, pattern) private
- #gather_directories(path) private
- #get_files_to_run(paths) private
- #get_matching_files(path, pattern) private
- #handle_suite_hook(scope, meta) private
- #load_file_handling_errors(method, file) private
- #metadata_applies_to_group?(meta, group) ⇒ Boolean private
- #on_existing_matching_groups(meta) private
- #output_to_tty?(output = output_stream) ⇒ Boolean private
- #output_wrapper private
- #paths_to_check(paths) private
- #run_suite_hooks(hook_description, hooks) private
-
#safe_extend(mod, host)
private
See additional method definition at line 2399.
-
#safe_include(mod, host)
private
:nocov:
- #safe_prepend(mod, host) private
- #update_pattern_attr(name, value) private
- #value_for(key) private
- #windows_absolute_network_path?(pattern) ⇒ Boolean private
Hooks
- Included
#after | Declare a block of code to be run after each example (using |
#append_after | Adds |
#append_before | Alias for Hooks#before. |
#around | Declare a block of code, parts of which will be run before and parts after the example. |
#before | Declare a block of code to be run before each example (using |
#hooks | Holds the various registered hooks. |
#prepend_after | Alias for Hooks#after. |
#prepend_before | Adds |
Constructor Details
.new ⇒ Configuration
Build an object to store runtime configuration options and set defaults
# File 'rspec-core/lib/rspec/core/configuration.rb', line 534
def initialize # rubocop:disable Style/GlobalVars @start_time = $_rspec_core_load_started_at || ::RSpec::Core::Time.now # rubocop:enable Style/GlobalVars @expectation_frameworks = [] @include_modules = FilterableItemRepository::QueryOptimized.new(:any?) @extend_modules = FilterableItemRepository::QueryOptimized.new(:any?) @prepend_modules = FilterableItemRepository::QueryOptimized.new(:any?) @bisect_runner = RSpec::Support::RubyFeatures.fork_supported? ? :fork : :shell @bisect_runner_class = nil @before_suite_hooks = [] @after_suite_hooks = [] @mock_framework = nil @files_or_directories_to_run = [] @loaded_spec_files = Set.new @color = false @color_mode = :automatic @pattern = '**{,/*/**}/*_spec.rb' @exclude_pattern = '' @failure_exit_code = 1 @error_exit_code = nil # so it can be overridden by failure exit code @fail_if_no_examples = false @spec_files_loaded = false @backtrace_formatter = BacktraceFormatter.new @default_path = 'spec' @project_source_dirs = %w[ spec lib app ] @deprecation_stream = $stderr @output_stream = $stdout @reporter = nil @reporter_buffer = nil @filter_manager = FilterManager.new @static_config_filter_manager = FilterManager.new @ordering_manager = Ordering::ConfigurationManager.new @preferred_options = {} @failure_color = :red @success_color = :green @pending_color = :yellow @default_color = :white @fixed_color = :blue @detail_color = :cyan @profile_examples = false @requires = [] @libs = [] @derived_metadata_blocks = FilterableItemRepository::QueryOptimized.new(:any?) @threadsafe = true @max_displayed_failure_line_count = 10 @full_cause_backtrace = false @world = World::Null @shared_context_metadata_behavior = :trigger_inclusion @pending_failure_output = :full @force_line_number_for_spec_rerun = false define_built_in_hooks end
Class Method Details
.add_read_only_setting(name, opts = {})
As .add_setting but only add the reader.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 100
def self.add_read_only_setting(name, opts={}) raise "Use the instance add_setting method if you want to set a default" if opts.key?(:default) define_reader name define_predicate name end
.add_setting(name, opts = {})
Invoked by the add_setting
instance method. Use that method on a Configuration
instance rather than this class method.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 87
def self.add_setting(name, opts={}) raise "Use the instance add_setting method if you want to set a default" if opts.key?(:default) attr_writer name add_read_only_setting name Array(opts[:alias_with]).each do |alias_name| define_alias(name, alias_name) end end
.define_alias(name, alias_name)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 70
def self.define_alias(name, alias_name) alias_method alias_name, name alias_method "#{alias_name}=", "#{name}=" define_predicate alias_name end
.define_predicate(name)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 77
def self.define_predicate(name) define_method "#{name}?" do !!send(name) end end
.define_reader(name)
.delegate_to_ordering_manager(*methods)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1678
def self.delegate_to_ordering_manager(*methods) methods.each do |method| define_method method do |*args, &block| ordering_manager.__send__(method, *args, &block) end end end
Instance Attribute Details
#backtrace_exclusion_patterns ⇒ Array
<Regexp
> (rw)
Regexps used to exclude lines from backtraces.
Excludes lines from ruby (and jruby) source, installed gems, anything in any “bin” directory, and any of the ::RSpec
libs (outside gem installs) by default.
You can modify the list via the getter, or replace it with the setter.
To override this behaviour and display a full backtrace, use –backtrace
on the command line, in a .rspec
file, or in the rspec_options
attribute of RSpec’s rake task.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 704
def backtrace_exclusion_patterns @backtrace_formatter.exclusion_patterns end
#backtrace_exclusion_patterns=(patterns) (rw)
Set
regular expressions used to exclude lines in backtrace.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 710
def backtrace_exclusion_patterns=(patterns) @backtrace_formatter.exclusion_patterns = patterns end
#backtrace_formatter (readonly)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 528
attr_reader :backtrace_formatter, :ordering_manager, :loaded_spec_files
#backtrace_inclusion_patterns ⇒ Array
<Regexp
> (rw)
Regexps used to include lines in backtraces.
Defaults to [Regexp.new Dir.getwd].
Lines that match an exclusion and an inclusion pattern will be included.
You can modify the list via the getter, or replace it with the setter.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 723
def backtrace_inclusion_patterns @backtrace_formatter.inclusion_patterns end
#backtrace_inclusion_patterns=(patterns) (rw)
Set
regular expressions used to include lines in backtrace.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 729
def backtrace_inclusion_patterns=(patterns) @backtrace_formatter.inclusion_patterns = patterns end
#bisect_runner ⇒ Symbol
(rw)
This option will only be used by –bisect
if you set it in a file loaded via –require
.
Determines which bisect runner implementation gets used to run subsets of the suite during a bisection. Your choices are:
- {:shell}: Performs a spec run by shelling out, booting RSpec and your
application environment each time. This runner is the most widely
compatible runner, but is not as fast. On platforms that do not
support forking, this is the default.
- {:fork}: Pre-boots RSpec and your application environment in a parent
process, and then forks a child process for each spec run. This runner
tends to be significantly faster than the {:shell} runner but cannot
be used in some situations. On platforms that support forking, this
is the default. If you use this runner, you should ensure that all
of your one-time setup logic goes in a `before(:suite)` hook instead
of getting run at the top-level of a file loaded by {--require}.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 505
attr_reader :bisect_runner
#bisect_runner=(value) (rw)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 506
def bisect_runner=(value) if @bisect_runner_class && value != @bisect_runner raise "`config.bisect_runner = #{value.inspect}` can no longer take " \ "effect as the #{@bisect_runner.inspect} bisect runnner is already " \ "in use. This config setting must be set in a file loaded by a " \ "`--require` option (passed at the CLI or in a `.rspec` file) for " \ "it to have any effect." end @bisect_runner = value end
#color ⇒ Boolean
(rw)
No longer recommended because of complex behavior. Instead, rely on the fact that TTYs will display color by default, or set #color_mode to :on
to display color on a non-TTY output.
Enables color output if the output is a TTY. As of ::RSpec
3.6, this is the default behavior and this option is retained only for backwards compatibility.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 929
def color value_for(:color) { @color } end
#color=(value) (rw)
No longer recommended because of complex behavior. Instead, rely on the fact that TTYs will display color by default, or set :color_mode
to :on
to display color on a non-TTY output.
Toggle output color.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 967
attr_writer :color
#color_mode ⇒ Boolean
(rw)
The mode for determining whether to display output in color. One of:
-
:automatic
- the output will be in color if the output is a TTY (the default) -
:on
- the output will be in color, whether or not the output is a TTY -
:off
- the output will not be in color
# File 'rspec-core/lib/rspec/core/configuration.rb', line 942
def color_mode value_for(:color_mode) { @color_mode } end
#color_mode=(value) (rw)
Set
the color mode.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 960
attr_writer :color_mode
#default_color ⇒ Symbol
(rw)
The default output color. Defaults to :white
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 358
add_setting :default_color
#default_formatter (rw)
The formatter that will be used if no formatter has been set. Defaults to ‘progress’.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1010
def default_formatter formatter_loader.default_formatter end
#default_formatter=(value) (rw)
Sets a fallback formatter to use if none other has been set.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1021
def default_formatter=(value) formatter_loader.default_formatter = value end
#default_path ⇒ String
(rw)
Other scripts invoking rspec
indirectly will ignore this setting.
Path to use if no path is provided to the rspec
command (default: “spec”
). Allows you to just type rspec
instead of ‘rspec spec` to run all the examples in the spec
directory.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 120
add_read_only_setting :default_path
#default_path=(path) ⇒ String
(rw)
Other scripts invoking rspec
indirectly will ignore this setting.
Path to use if no path is provided to the rspec
command (default: “spec”
). Allows you to just type rspec
instead of ‘rspec spec` to run all the examples in the spec
directory.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 121
add_read_only_setting :default_path
#deprecation_stream ⇒ IO
, String
(rw)
Determines where deprecation warnings are printed. Defaults to $stderr
.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 164
define_reader :deprecation_stream
#deprecation_stream=(value) (rw)
Determines where deprecation warnings are printed.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 168
def deprecation_stream=(value) if @reporter && !value.equal?(@deprecation_stream) warn "RSpec's reporter has already been initialized with " \ "#{deprecation_stream.inspect} as the deprecation stream, so your change to "\ "`deprecation_stream` will be ignored. You should configure it earlier for " \ "it to take effect, or use the `--deprecation-out` CLI option. " \ "(Called from #{CallerFilter.first_non_rspec_line})" else @deprecation_stream = value end end
#detail_color ⇒ Symbol
(rw)
Color used to print details. Defaults to :cyan
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 372
add_setting :detail_color
#disable_monkey_patching (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1888
attr_accessor :disable_monkey_patching
#drb ⇒ Boolean
(rw)
Run examples over DRb (default: false
). ::RSpec
doesn’t supply the DRb server, but you can use tools like spork.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 130
add_setting :drb
#drb_port (rw)
The drb_port (default: nil).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 134
add_setting :drb_port
#dry_run (rw)
Prints the formatter output of your suite without running any examples or hooks.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 238
add_setting :dry_run
#error_exit_code ⇒ Integer
(rw)
The exit code to return if there are any errors outside examples (default: failure_exit_code)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 248
add_setting :error_exit_code
#error_stream (rw)
Default: $stderr
.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 138
add_setting :error_stream
#example_status_persistence_file_path ⇒ String
(rw)
#example_status_persistence_file_path=(value)
String
(rw)
#example_status_persistence_file_path=(value)
The file path to use for persisting example statuses. Necessary for the –only-failures
and –next-failure
CLI options.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 188
define_reader :example_status_persistence_file_path
#example_status_persistence_file_path=(value) (rw)
Sets the file path to use for persisting example statuses. Necessary for the –only-failures
and –next-failure
CLI options.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 192
def example_status_persistence_file_path=(value) @example_status_persistence_file_path = value clear_values_derived_from_example_status_persistence_file_path end
#exclude_pattern ⇒ String
(rw)
Exclude files matching this pattern.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 300
define_reader :exclude_pattern
#exclude_pattern=(value) (rw)
Set
pattern to match files to exclude.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 304
def exclude_pattern=(value) update_pattern_attr :exclude_pattern, value end
#exclusion_filter (rw)
Returns the exclusion_filter
. If none has been set, returns an empty hash.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1376
def exclusion_filter filter_manager.exclusions end
#exclusion_filter=(filter) (rw)
Clears and reassigns the #exclusion_filter. Set
to nil
if you don’t want any exclusion filter at all.
Warning
This overrides any exclusion filters/tags set on the command line or in configuration files.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1369
def exclusion_filter=(filter) = Metadata.build_hash_from([filter], :warn_about_example_group_filtering) filter_manager.exclude_only end
#expectation_framework=(framework) (writeonly)
Delegates to expect_with(framework).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 838
def expectation_framework=(framework) expect_with(framework) end
#expose_dsl_globally=(value) (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 151
def expose_dsl_globally=(value) if value Core::DSL.expose_globally! Core::SharedExampleGroup::TopLevelDSL.expose_globally! else Core::DSL.remove_globally! Core::SharedExampleGroup::TopLevelDSL.remove_globally! end end
#expose_dsl_globally? ⇒ Boolean
(rw)
Indicates if the DSL
has been exposed off of modules and main
. Default: true
# File 'rspec-core/lib/rspec/core/configuration.rb', line 143
def expose_dsl_globally? Core::DSL.exposed_globally? end
#fail_fast (rw)
If specified, indicates the number of failures required before cleaning up and exit (default: nil
). Can also be true
to fail and exit on first failure
# File 'rspec-core/lib/rspec/core/configuration.rb', line 211
define_reader :fail_fast
#fail_fast=(value) (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 214
def fail_fast=(value) case value when true, 'true' @fail_fast = true when false, 'false', 0 @fail_fast = false when nil @fail_fast = nil else @fail_fast = value.to_i if value.to_i == 0 # TODO: in RSpec 4, consider raising an error here. RSpec.warning "Cannot set `RSpec.configuration.fail_fast`" \ " to `#{value.inspect}`. Only `true`, `false`, `nil` and integers" \ " are valid values." @fail_fast = true end end end
#fail_if_no_examples ⇒ Boolean
(rw)
Whether or not to fail when there are no ::RSpec
examples (default: false).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 253
add_setting :fail_if_no_examples
#failure_color ⇒ Symbol
(rw)
Color to use to indicate failure. Defaults to :red
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 351
add_setting :failure_color
#failure_exit_code ⇒ Integer
(rw)
The exit code to return if there are any failures (default: 1).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 243
add_setting :failure_exit_code
#files_to_run ⇒ Array
(rw)
The spec files ::RSpec
will run.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1103
def files_to_run @files_to_run ||= get_files_to_run(@files_or_directories_to_run) end
#files_to_run=(value) (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 522
attr_writer :files_to_run
#filter (rw)
Alias for #inclusion_filter.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1324
alias_method :filter, :inclusion_filter
#filter_manager (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 524
attr_accessor :filter_manager, :world
#fixed_color ⇒ Symbol
(rw)
Color used when a pending example is fixed. Defaults to :blue
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 365
add_setting :fixed_color
#full_backtrace=(true_or_false) (rw)
Toggle full backtrace.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 915
def full_backtrace=(true_or_false) @backtrace_formatter.full_backtrace = true_or_false end
#full_backtrace? ⇒ Boolean
(rw)
Check if full backtrace is enabled.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 909
def full_backtrace? @backtrace_formatter.full_backtrace? end
#full_description ⇒ Array
(rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 984
def full_description filter.fetch :full_description, nil end
#full_description=(description) (rw)
Run examples matching on description
in all files to run.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 979
def full_description=(description) filter_run :full_description => Regexp.union(*Array(description).map { |d| Regexp.new(d) }) end
#inclusion_filter (rw) Also known as: #filter
Returns the inclusion_filter
. If none has been set, returns an empty hash.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1320
def inclusion_filter filter_manager.inclusions end
#inclusion_filter=(filter) (rw) Also known as: #filter=
Clears and reassigns the #inclusion_filter. Set
to nil
if you don’t want any inclusion filter at all.
Warning
This overrides any inclusion filters/tags set on the command line or in configuration files.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1311
def inclusion_filter=(filter) = Metadata.build_hash_from([filter], :warn_about_example_group_filtering) filter_manager.include_only end
#libs ⇒ Array
<String
> (rw)
Returns dirs that have been prepended to the load path by the -I
command line option.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 264
define_reader :libs
#libs=(libs) (rw)
#loaded_spec_files (readonly)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 528
attr_reader :backtrace_formatter, :ordering_manager, :loaded_spec_files
#max_displayed_failure_line_count (rw)
Maximum count of failed source lines to display in the failure reports (defaults to 10
). return [Integer]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 459
add_setting :max_displayed_failure_line_count
#mock_framework ⇒ Symbol
(rw)
Returns the configured mock framework adapter module.
#mock_framework=(framework) (rw)
Delegates to mock_framework=(framework).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 688
def mock_framework=(framework) mock_with framework end
#only_failures? (readonly)
Alias for #only_failures.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 200
alias_method :only_failures?, :only_failures
#only_failures_but_not_configured? ⇒ Boolean
(readonly)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 203
def only_failures_but_not_configured? only_failures? && !example_status_persistence_file_path end
#ordering_manager (readonly)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 528
attr_reader :backtrace_formatter, :ordering_manager, :loaded_spec_files
#output_stream ⇒ IO
, String
(rw)
Determines where ::RSpec
will send its output. Default: $stdout
.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 270
define_reader :output_stream
#output_stream=(value) (rw)
Set
the output stream for reporter.
See additional method definition at line 270.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 274
define_reader :output_stream
#pattern ⇒ String
(rw)
Load files matching this pattern (default: ‘’**,//*
/*_spec.rb’‘).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 289
define_reader :pattern
#pattern=(value) (rw)
Set
pattern to match files to load.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 293
def pattern=(value) update_pattern_attr :pattern, value end
#pattern_might_load_specs_from_vendored_dirs? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2228
def pattern_might_load_specs_from_vendored_dirs? pattern.split(File::SEPARATOR).first.include?('**') end
#pending_color ⇒ Symbol
(rw)
Color to use to print pending examples. Defaults to :yellow
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 344
add_setting :pending_color
#pending_failure_output (rw)
Format the output for pending examples. Can be set to:
- :full (default) - pending examples appear similarly to failures
- :no_backtrace - same as above, but with no backtrace
- :skip - do not show the section at all
return [Symbol]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 472
add_read_only_setting :pending_failure_output
#pending_failure_output=(mode) (rw)
Format the output for pending examples. Can be set to:
- :full (default) - pending examples appear similarly to failures
- :no_backtrace - same as above, but with no backtrace
- :skip - do not show the section at all
return [Symbol]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 473
add_read_only_setting :pending_failure_output
#profile_examples ⇒ Boolean
(rw)
Report the times for the slowest examples (default: false
). Use this to specify the number of examples to include in the profile.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1080
def profile_examples profile = value_for(:profile_examples) { @profile_examples } if profile && !profile.is_a?(Integer) 10 else profile end end
#profile_examples=(value) ⇒ Boolean
(rw)
Report the times for the slowest examples (default: false
). Use this to specify the number of examples to include in the profile.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 322
attr_writer :profile_examples
#project_source_dirs ⇒ Array
<String
> (rw)
Specifies which directories contain the source code for your project. When a failure occurs, ::RSpec
looks through the backtrace to find a a line of source to print. It first looks for a line coming from one of the project source directories so that, for example, it prints the expectation or assertion call rather than the source code from the expectation or assertion framework.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 316
add_setting :project_source_dirs
#raise_on_warning=(value) (writeonly)
Turns RSpec warnings into errors. This can be useful when you want ::RSpec
to run in a ‘strict’ no warning situation. (Note this does not capture or raise on Ruby warnings).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1788
def raise_on_warning=(value) if value RSpec::Support.warning_notifier = RAISE_ERROR_WARNING_NOTIFIER else RSpec::Support.warning_notifier = RSpec::Support::DEFAULT_WARNING_NOTIFIER end end
#requires ⇒ Array
<String
> (rw)
Indicates files configured to be required.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 258
define_reader :requires
#requires=(paths) (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1604
def requires=(paths) directories = ['lib', default_path].select { |p| File.directory? p } RSpec::Core::RubyProject.add_to_load_path(*directories) paths.each { |path| load_file_handling_errors(:require, path) @requires << path } end
#rspec_expectations_loaded? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2337
def rspec_expectations_loaded? defined?(RSpec::Expectations.configuration) end
#rspec_mocks_loaded? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2333
def rspec_mocks_loaded? defined?(RSpec::Mocks.configuration) end
#run_all_when_everything_filtered (rw)
Use #filter_run_when_matching instead for the specific filters that you want to be ignored if none match.
Run all examples if none match the configured filters (default: false
).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 330
add_setting :run_all_when_everything_filtered
#silence_filter_announcements (rw)
Don’t print filter info i.e. “Run options: include :focus=>true
” (default false
). return [Boolean]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 378
add_setting :silence_filter_announcements
#static_config_filter_manager (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 526
attr_accessor :static_config_filter_manager
#success_color ⇒ Symbol
(rw)
Color to use to indicate success. Defaults to :green
but can be set to one of the following: ‘[:black, :white
, :red
, :green
, :yellow
, :blue
, :magenta
, :cyan]`
# File 'rspec-core/lib/rspec/core/configuration.rb', line 337
add_setting :success_color
#threadsafe (rw)
Use threadsafe options where available. Currently this will place a mutex around memoized values such as let blocks. return [Boolean]
# File 'rspec-core/lib/rspec/core/configuration.rb', line 453
add_setting :threadsafe
#treat_symbols_as_metadata_keys_with_true_values=(_value) (writeonly)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 383
def (_value) RSpec.deprecate( "RSpec::Core::Configuration#treat_symbols_as_metadata_keys_with_true_values=", : => "RSpec::Core::Configuration#treat_symbols_as_metadata_keys_with_true_values= " \ "is deprecated, it is now set to true as default and " \ "setting it to false has no effect." ) end
#warnings=(value) (rw)
Set
Ruby warnings on or off.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1767
def warnings=(value) $VERBOSE = !!value end
#warnings? ⇒ Boolean
(rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1772
def warnings? $VERBOSE end
#world (rw)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 524
attr_accessor :filter_manager, :world
Instance Method Details
#absolute_pattern?(pattern) ⇒ Boolean
(private)
:nocov:
See additional method definition at line 2251.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2261
def absolute_pattern?(pattern) pattern =~ /\A[A-Z]:\\/ || windows_absolute_network_path?(pattern) end
#add_formatter(formatter)
#add_formatter(formatter, output)
Also known as: #formatter=
Adds a formatter to the set ::RSpec
will use for this run.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1003
def add_formatter(formatter, output=output_wrapper) formatter_loader.add(formatter, output) end
#add_hook_to_existing_matching_groups(meta, scope, &block) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2362
def add_hook_to_existing_matching_groups(, scope, &block) # For example hooks, we have to apply it to each of the top level # groups, even if the groups do not match. When we apply it, we # apply it with the metadata, so it will only apply to examples # in the group that match the metadata. # #2280 for background and discussion. if scope == :example || scope == :each || scope.nil? world.example_groups.each(&block) else = Metadata.build_hash_from( .dup) on_existing_matching_groups(, &block) end end
#add_setting(name)
#add_setting(name, opts)
Adds a custom setting to the ::RSpec
.configuration object.
RSpec.configuration.add_setting :foo
Used internally and by extension frameworks like rspec-rails, so they can add config settings that are domain specific. For example:
RSpec.configure do |c|
c.add_setting :use_transactional_fixtures,
:default => true,
:alias_with => :use_transactional_examples
end
add_setting
creates three methods on the configuration object, a setter, a getter, and a predicate:
RSpec.configuration.foo=(value)
RSpec.configuration.foo
RSpec.configuration.foo? # Returns true if foo returns anything but nil or false.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 666
def add_setting(name, opts={}) default = opts.delete(:default) (class << self; self; end).class_exec do add_setting(name, opts) end __send__("#{name}=", default) if default end
#after(scope = nil, *meta, &block) Also known as: #prepend_after
Defines a after
hook. See Hooks#after for full docs.
This method differs from Hooks#after in only one way: it supports the :suite
scope. Hooks
with the :suite
scope will be run once after the last example of the entire suite is executed. Conditions passed along with :suite
are effectively ignored.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2038
def after(scope=nil, *, &block) handle_suite_hook(scope, ) do @after_suite_hooks.unshift Hooks::AfterHook.new(block, {}) end || begin # defeat Ruby 2.5 lazy proc allocation to ensure # the methods below are passed the same proc instances # so `Hook` equality is preserved. For more info, see: # https://bugs.ruby-lang.org/issues/14045#note-5 block.__id__ add_hook_to_existing_matching_groups(, scope) { |g| g.after(scope, *, &block) } super(scope, *, &block) end end
#alias_example_group_to(new_name, *args)
The defined aliased will also be added to the top level (e.g. main
and from within modules) if expose_dsl_globally
is set to true.
Creates a method that defines an example group with the provided metadata. Can be used to define example group/metadata shortcuts.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1216
def alias_example_group_to(new_name, *args) = Metadata.build_hash_from(args) RSpec::Core::ExampleGroup.define_example_group_method(new_name, ) end
#alias_example_to(name, *args)
The specific example alias below (‘pending`) is already defined for you.
Use with caution. This extends the language used in your specs, but does not add any additional documentation. We use this in ::RSpec
to define methods like focus
and xit
, but we also add docs for those methods.
Creates a method that delegates to example
including the submitted args
. Used internally to add variants of example
like pending
:
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1184
def alias_example_to(name, *args) = Metadata.build_hash_from(args) RSpec::Core::ExampleGroup.define_example_method(name, ) end
#alias_it_behaves_like_to(new_name, report_label = '') Also known as: #alias_it_should_behave_like_to
Use with caution. This extends the language used in your specs, but does not add any additional documentation. We use this in ::RSpec
to define it_should_behave_like
(for backward compatibility), but we also add docs for that method.
Define an alias for it_should_behave_like that allows different language (like “it_has_behavior” or “it_behaves_like”) to be employed when including shared examples.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1247
def alias_it_behaves_like_to(new_name, report_label='') RSpec::Core::ExampleGroup.define_nested_shared_group_method(new_name, report_label) end
#alias_it_should_behave_like_to(new_name, report_label = '')
Alias for #alias_it_behaves_like_to.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1250
alias_method :alias_it_should_behave_like_to, :alias_it_behaves_like_to
#append_after(scope = nil, *meta, &block)
Adds block
to the end of the list of #after blocks in the same scope (‘:example`, :context
, or :suite
), in contrast to #after, which adds the hook to the start of the list.
See Hooks#after for full #after hook docs.
This method differs from Hooks#append_after in only one way: it supports the :suite
scope. Hooks
with the :suite
scope will be run once after the last example of the entire suite is executed. Conditions passed along with :suite
are effectively ignored.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2068
def append_after(scope=nil, *, &block) handle_suite_hook(scope, ) do @after_suite_hooks << Hooks::AfterHook.new(block, {}) end || begin # defeat Ruby 2.5 lazy proc allocation to ensure # the methods below are passed the same proc instances # so `Hook` equality is preserved. For more info, see: # https://bugs.ruby-lang.org/issues/14045#note-5 block.__id__ add_hook_to_existing_matching_groups(, scope) { |g| g.append_after(scope, *, &block) } super(scope, *, &block) end end
#append_before(scope = nil, *meta, &block)
Alias for #before.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1997
alias_method :append_before, :before
#apply_derived_metadata_to(metadata)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1948
def ( ) already_run_blocks = Set.new # We loop and attempt to re-apply metadata blocks to support cascades # (e.g. where a derived bit of metadata triggers the application of # another piece of derived metadata, etc) # # We limit our looping to 200 times as a way to detect infinitely recursing derived metadata blocks. # It's hard to imagine a valid use case for a derived metadata cascade greater than 200 iterations. 200.times do return if @derived_metadata_blocks.items_for( ).all? do |block| already_run_blocks.include?(block).tap do |skip_block| block.call( ) unless skip_block already_run_blocks << block end end end # If we got here, then `@derived_metadata_blocks.items_for(metadata).all?` never returned # `true` above and we treat this as an attempt to recurse infinitely. It's better to fail # with a clear # error than hang indefinitely, which is what would happen if we didn't limit # the looping above. raise SystemStackError, "Attempted to recursively derive metadata indefinitely." end
#around(scope = nil, *meta, &block)
Registers block
as an around
hook.
See Hooks#around for full around
hook docs.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2086
def around(scope=nil, *, &block) # defeat Ruby 2.5 lazy proc allocation to ensure # the methods below are passed the same proc instances # so `Hook` equality is preserved. For more info, see: # https://bugs.ruby-lang.org/issues/14045#note-5 block.__id__ add_hook_to_existing_matching_groups(, scope) { |g| g.around(scope, *, &block) } super(scope, *, &block) end
#assert_no_example_groups_defined(config_option) (private)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2301
def assert_no_example_groups_defined(config_option) return unless world.example_groups.any? raise MustBeConfiguredBeforeExampleGroupsError.new( "RSpec's #{config_option} configuration option must be configured before " \ "any example groups are defined, but you have already defined a group." ) end
#before(scope = nil, *meta, &block) Also known as: #append_before
Defines a before
hook. See Hooks#before for full docs.
This method differs from Hooks#before in only one way: it supports the :suite
scope. Hooks
with the :suite
scope will be run once before the first example of the entire suite is executed. Conditions passed along with :suite
are effectively ignored.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1983
def before(scope=nil, *, &block) handle_suite_hook(scope, ) do @before_suite_hooks << Hooks::BeforeHook.new(block, {}) end || begin # defeat Ruby 2.5 lazy proc allocation to ensure # the methods below are passed the same proc instances # so `Hook` equality is preserved. For more info, see: # https://bugs.ruby-lang.org/issues/14045#note-5 block.__id__ add_hook_to_existing_matching_groups(, scope) { |g| g.before(scope, *, &block) } super(scope, *, &block) end end
#bisect_runner_class
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2132
def bisect_runner_class @bisect_runner_class ||= begin case bisect_runner when :fork RSpec::Support.require_rspec_core 'bisect/fork_runner' Bisect::ForkRunner when :shell RSpec::Support.require_rspec_core 'bisect/shell_runner' Bisect::ShellRunner else raise "Unsupported value for `bisect_runner` (#{bisect_runner.inspect}). " \ "Only `:fork` and `:shell` are supported." end end end
#clear_values_derived_from_example_status_persistence_file_path (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2351
def clear_values_derived_from_example_status_persistence_file_path @last_run_statuses = nil @spec_files_with_failures = nil end
#color_enabled?(output = output_stream) ⇒ Boolean
Check if color is enabled for a particular output.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 950
def color_enabled?(output=output_stream) case color_mode when :on then true when :off then false else # automatic output_to_tty?(output) || (color && tty?) end end
#command (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2283
def command $0.split(File::SEPARATOR).last end
#conditionally_disable_expectations_monkey_patching (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2327
def conditionally_disable_expectations_monkey_patching return unless disable_monkey_patching && rspec_expectations_loaded? RSpec::Expectations.configuration.syntax = :expect end
#conditionally_disable_mocks_monkey_patching (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2318
def conditionally_disable_mocks_monkey_patching return unless disable_monkey_patching && rspec_mocks_loaded? RSpec::Mocks.configuration.tap do |config| config.syntax = :expect config.patch_marshal_to_support_partial_doubles = false end end
#configure_example(example, example_hooks)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1587
def configure_example(example, example_hooks) example_hooks.register_global_singleton_context_hooks(example, hooks) singleton_group = example.example_group_instance.singleton_class # We replace the metadata so that SharedExampleGroupModule#included # has access to the example's metadata[:location]. singleton_group. (example. ) do modules = @include_modules.items_for(example. ) modules.each do |mod| safe_include(mod, example.example_group_instance.singleton_class) end MemoizedHelpers.define_helpers_on(singleton_group) unless modules.empty? end end
#configure_expectation_framework
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1629
def configure_expectation_framework expectation_frameworks.each do |framework| RSpec::Core::ExampleGroup.__send__(:include, framework) end conditionally_disable_expectations_monkey_patching end
#configure_group(group)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1575
def configure_group(group) group.hooks.register_globals(group, hooks) configure_group_with group, @include_modules, :safe_include configure_group_with group, @extend_modules, :safe_extend configure_group_with group, @prepend_modules, :safe_prepend end
#configure_group_with(group, module_list, application_method) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2356
def configure_group_with(group, module_list, application_method) module_list.items_for(group. ).each do |mod| __send__(application_method, mod, group) end end
#configure_mock_framework
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1623
def configure_mock_framework RSpec::Core::ExampleGroup.__send__(:include, mock_framework) conditionally_disable_mocks_monkey_patching end
#define_built_in_hooks (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2291
def define_built_in_hooks around(:example, :aggregate_failures => true) do |procsy| begin aggregate_failures(nil, :hide_backtrace => true, &procsy) rescue Support::AllExceptionsExceptOnesWeMustNotRescue => exception procsy.example.set_aggregate_failures_exception(exception) end end end
#define_derived_metadata(*filters) {|metadata| ... }
Defines a callback that can assign derived metadata values.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1907
def (*filters, &block) = Metadata.build_hash_from(filters, :warn_about_example_group_filtering) @derived_metadata_blocks.append(block, ) end
#define_mixed_in_module(mod, filters, mod_list, config_method, &block) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2414
def define_mixed_in_module(mod, filters, mod_list, config_method, &block) unless Module === mod raise TypeError, "`RSpec.configuration.#{config_method}` expects a module but got: #{mod.inspect}" end = Metadata.build_hash_from(filters, :warn_about_example_group_filtering) mod_list.append(mod, ) on_existing_matching_groups(, &block) end
#disable_monkey_patching!
It configures rspec-mocks and rspec-expectations only if the user is using those (either explicitly or implicitly by not setting #mock_with or #expect_with to anything else).
If the user uses this options with ‘mock_with :mocha` (or similar) they will still have monkey patching active in their test environment from mocha.
Enables zero monkey patching mode for ::RSpec
. It removes monkey patching of the top-level DSL
methods (‘describe`, shared_examples_for
, etc) onto main
and Module
, instead requiring you to prefix these methods with RSpec.
. It enables expect-only syntax for rspec-mocks and rspec-expectations. It simply disables monkey patching on whatever pieces of ::RSpec
the user is using.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1880
def disable_monkey_patching! self.expose_dsl_globally = false self.disable_monkey_patching = true conditionally_disable_mocks_monkey_patching conditionally_disable_expectations_monkey_patching end
#expect_with(*frameworks)
Sets the expectation framework module(s) to be included in each example group.
frameworks
can be :rspec
, :test_unit
, :minitest
, a custom module, or any combination thereof:
config.expect_with :rspec
config.expect_with :test_unit
config.expect_with :minitest
config.expect_with :rspec, :minitest
config.expect_with OtherExpectationFramework
::RSpec
will translate :rspec
, :minitest
, and :test_unit
into the appropriate modules.
Configuration
If the module responds to RSpec.configuration, expect_with
will yield the RSpec.configuration object if given a block:
config.expect_with OtherExpectationFramework do |custom_config|
custom_config.custom_setting = true
end
# File 'rspec-core/lib/rspec/core/configuration.rb', line 865
def expect_with(*frameworks) modules = frameworks.map do |framework| case framework when Module framework when :rspec require 'rspec/expectations' # Tag this exception class so our exception formatting logic knows # that it satisfies the `MultipleExceptionError` interface. ::RSpec::Expectations::MultipleExpectationsNotMetError.__send__( :include, MultipleExceptionError::InterfaceTag ) ::RSpec::Matchers when :test_unit require 'rspec/core/test_unit_assertions_adapter' ::RSpec::Core::TestUnitAssertionsAdapter when :minitest require 'rspec/core/minitest_assertions_adapter' ::RSpec::Core::MinitestAssertionsAdapter else raise ArgumentError, "#{framework.inspect} is not supported" end end if (modules - @expectation_frameworks).any? assert_no_example_groups_defined(:expect_with) end if block_given? raise "expect_with only accepts a block with a single argument. " \ "Call expect_with #{modules.length} times, " \ "once with each argument, instead." if modules.length > 1 raise "#{modules.first} must respond to `configuration` so that " \ "expect_with can yield it." unless modules.first.respond_to?(:configuration) yield modules.first.configuration end @expectation_frameworks.push(*modules) end
#expectation_frameworks
Returns the configured expectation framework adapter module(s)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 826
def expectation_frameworks if @expectation_frameworks.empty? begin expect_with :rspec rescue LoadError expect_with Module.new end end @expectation_frameworks end
#expose_current_running_example_as(method_name)
Exposes the current running example via the named helper method. ::RSpec
2.x exposed this via example
, but in ::RSpec
3.0, the example is instead exposed via an arg yielded to it
, #before, let
, etc. However, some extension gems (such as Capybara) depend on the ::RSpec
2.x’s example
method, so this config option can be used to maintain compatibility.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1818
def expose_current_running_example_as(method_name) ExposeCurrentExample.module_exec do extend RSpec::SharedContext let(method_name) { |ex| ex } end include ExposeCurrentExample end
#extend(mod, *filters)
Tells RSpec to extend example groups with mod
. Methods defined in mod
are exposed to example groups (not examples). Use filters
to constrain the groups to extend.
Similar to #include, but behavior is added to example groups, which are classes, rather than the examples, which are instances of those classes.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1525
def extend(mod, *filters) define_mixed_in_module(mod, filters, @extend_modules, :extend) do |group| safe_extend(mod, group) end end
#extract_location(path) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2266
def extract_location(path) match = /^(.*?)((?:\:\d+)+)$/.match(path) if match captures = match.captures path = captures[0] lines = captures[1][1..-1].split(":").map(&:to_i) filter_manager.add_location path, lines else path, scoped_ids = Example.parse_id(path) filter_manager.add_ids(path, scoped_ids.split(/\s*,\s*/)) if scoped_ids end return [] if path == default_path File. (path) end
#file_glob_from(path, pattern) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2243
def file_glob_from(path, pattern) stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}" return stripped if pattern =~ /^(\.\/)?#{Regexp.escape path}/ || absolute_pattern?(pattern) File.join(path, stripped) end
#files_or_directories_to_run=(*files)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1090
def files_or_directories_to_run=(*files) files = files.flatten if (command == 'rspec' || Runner.running_in_drb?) && default_path && files.empty? files << default_path end @files_or_directories_to_run = files @files_to_run = nil end
#filter_gems_from_backtrace(*gem_names)
The patterns this adds will match the named gems in their common locations (e.g. system gems, vendored with bundler, installed as a :git
dependency with bundler, etc) but is not guaranteed to work for all possible gem locations. For example, if you have the gem source in a directory with a completely unrelated name, and use bundler’s :path
option, this will not filter it.
Adds #backtrace_exclusion_patterns that will filter lines from the named gems from backtraces.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 749
def filter_gems_from_backtrace(*gem_names) gem_names.each do |name| @backtrace_formatter.filter_gem(name) end end
#filter_run(*args)
Alias for #filter_run_including.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1287
alias_method :filter_run, :filter_run_including
#filter_run_excluding(*args)
Adds key/value pairs to the #exclusion_filter. If args
includes any symbols that are not part of the hash, each symbol is treated as a key in the hash with the value true
.
Note
Filters set using this method can be overridden from the command line or config files (e.g. .rspec
).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1356
def filter_run_excluding(*args) = Metadata.build_hash_from(args, :warn_about_example_group_filtering) filter_manager.exclude_with_low_priority static_config_filter_manager.exclude_with_low_priority Metadata.deep_hash_dup( ) end
#filter_run_including(*args) Also known as: #filter_run
Adds key/value pairs to the #inclusion_filter. If args
includes any symbols that are not part of the hash, each symbol is treated as a key in the hash with the value true
.
Note
Filters set using this method can be overridden from the command line or config files (e.g. .rspec
).
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1282
def filter_run_including(*args) = Metadata.build_hash_from(args, :warn_about_example_group_filtering) filter_manager.include_with_low_priority static_config_filter_manager.include_with_low_priority Metadata.deep_hash_dup( ) end
#filter_run_when_matching(*args)
Applies the provided filter only if any of examples match, in constrast to #filter_run, which always applies even if no examples match, in which case no examples will be run. This allows you to leave configured filters in place that are intended only for temporary use. The most common example is focus filtering: ‘config.filter_run_when_matching :focus`. With that configured, you can temporarily focus an example or group by tagging it with :focus
metadata, or prefixing it with an f
(as in fdescribe
, fcontext
and fit
) since those are aliases for `describe`/`context`/`it` with :focus
metadata.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1298
def filter_run_when_matching(*args) when_first_matching_example_defined(*args) do filter_run(*args) end end
#force(hash)
Used to set higher priority option values from the command line.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 599
def force(hash) ordering_manager.force(hash) @preferred_options.merge!(hash) return unless hash.key?(:example_status_persistence_file_path) clear_values_derived_from_example_status_persistence_file_path end
#format_docstrings(&block)
Formats the docstring output using the block provided.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1668
def format_docstrings(&block) @format_docstrings_block = block_given? ? block : DEFAULT_FORMATTER end
#format_docstrings_block
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1673
def format_docstrings_block @format_docstrings_block ||= DEFAULT_FORMATTER end
#formatter=(formatter, output = output_wrapper)
Alias for #add_formatter.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1006
alias_method :formatter=, :add_formatter
#formatter_loader
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1036
def formatter_loader @formatter_loader ||= Formatters::Loader.new(Reporter.new(self)) end
#formatters ⇒ Array
Returns a duplicate of the formatters currently loaded in the FormatterLoader
for introspection.
Note as this is a duplicate, any mutations will be disregarded.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1031
def formatters formatter_loader.formatters.dup end
#gather_directories(path) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2232
def gather_directories(path) include_files = get_matching_files(path, pattern) exclude_files = get_matching_files(path, exclude_pattern) (include_files - exclude_files).uniq end
#get_files_to_run(paths) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2211
def get_files_to_run(paths) files = FlatMap.flat_map(paths_to_check(paths)) do |path| path = path.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR File.directory?(path) ? gather_directories(path) : extract_location(path) end.uniq return files unless only_failures? relative_files = files.map { |f| Metadata.relative_path(File. f) } intersection = (relative_files & spec_files_with_failures.to_a) intersection.empty? ? files : intersection end
#get_matching_files(path, pattern) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2238
def get_matching_files(path, pattern) raw_files = Dir[file_glob_from(path, pattern)] raw_files.map { |file| File. (file) }.sort end
#handle_suite_hook(scope, meta) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2178
def handle_suite_hook(scope, ) return nil unless scope == :suite unless .empty? # TODO: in RSpec 4, consider raising an error here. # We warn only for backwards compatibility. RSpec.warn_with "WARNING: `:suite` hooks do not support metadata since " \ "they apply to the suite as a whole rather than " \ "any individual example or example group that has metadata. " \ "The metadata you have provided (#{ .inspect}) will be ignored." end yield end
#hooks
Holds the various registered hooks. Here we use a FilterableItemRepository
implementation that is specifically optimized for the read/write patterns of the config object.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2116
def hooks @hooks ||= HookCollections.new(self, FilterableItemRepository::QueryOptimized) end
#in_project_source_dir_regex
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1614
def in_project_source_dir_regex regexes = project_source_dirs.map do |dir| /\A#{Regexp.escape(File. (dir))}\// end Regexp.union(regexes) end
#include(mod, *filters)
Filtered module inclusions can also be applied to individual examples that have matching metadata. Just like Ruby’s object model is that every object has a singleton class which has only a single instance, RSpec’s model is that every example has a singleton example group containing just the one example.
Tells RSpec to include mod
in example groups. Methods defined in mod
are exposed to examples (not example groups). Use filters
to constrain the groups or examples in which to include the module.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1435
def include(mod, *filters) define_mixed_in_module(mod, filters, @include_modules, :include) do |group| safe_include(mod, group) end end
#include_context(shared_group_name, *filters)
Filtered context inclusions can also be applied to individual examples that have matching metadata. Just like Ruby’s object model is that every object has a singleton class which has only a single instance, RSpec’s model is that every example has a singleton example group containing just the one example.
Tells RSpec to include the named shared example group in example groups. Use filters
to constrain the groups or examples in which to include the example group.
#last_run_statuses
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1108
def last_run_statuses @last_run_statuses ||= Hash.new(UNKNOWN_STATUS).tap do |statuses| if (path = example_status_persistence_file_path) begin ExampleStatusPersister.load_from(path).inject(statuses) do |hash, example| status = example[:status] status = UNKNOWN_STATUS unless VALID_STATUSES.include?(status) hash[example.fetch(:example_id)] = status hash end rescue SystemCallError => e RSpec.warning "Could not read from #{path.inspect} (configured as " \ "`config.example_status_persistence_file_path`) due " \ "to a system error: #{e.inspect}. Please check that " \ "the config option is set to an accessible, valid " \ "file path", :call_site => nil end end end end
#load_file_handling_errors(method, file) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2150
def load_file_handling_errors(method, file) __send__(method, file) rescue LoadError => ex relative_file = Metadata.relative_path(file) suggestions = DidYouMean.new(relative_file).call reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.#{suggestions}") RSpec.world.wants_to_quit = true rescue SyntaxError => ex relative_file = Metadata.relative_path(file) reporter.notify_non_example_exception( ex, "While loading #{relative_file} a `raise SyntaxError` occurred, RSpec will now quit." ) RSpec.world.rspec_is_quitting = true rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex relative_file = Metadata.relative_path(file) reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.") RSpec.world.wants_to_quit = true rescue SystemExit => ex relative_file = Metadata.relative_path(file) reporter.notify_non_example_exception( ex, "While loading #{relative_file} an `exit` / `raise SystemExit` occurred, RSpec will now quit." ) RSpec.world.rspec_is_quitting = true raise ex end
#load_spec_files
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1637
def load_spec_files # Note which spec files world is already aware of. # This is generally only needed for when the user runs # `ruby path/to/spec.rb` (and loads `rspec/autorun`) -- # in that case, the spec file was loaded by `ruby` and # isn't loaded by us here so we only know about it because # of an example group being registered in it. world.registered_example_group_files.each do |f| loaded_spec_files << f # the registered files are already expended absolute paths end files_to_run.uniq.each do |f| file = File. (f) load_file_handling_errors(:load, file) loaded_spec_files << file end @spec_files_loaded = true end
#metadata_applies_to_group?(meta, group) ⇒ Boolean
(private)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2384
def (, group) .empty? || MetadataFilter.apply?(:any?, , group. ) end
#mock_with(framework)
Sets the mock framework adapter module.
framework
can be a Symbol or a Module.
Given any of :rspec
, :mocha
, :flexmock
, or :rr
, configures the named framework.
Given :nothing
, configures no framework. Use this if you don’t use any mocking framework to save a little bit of overhead.
Given a Module, includes that module in every example group. The module should adhere to RSpec’s mock framework adapter API:
setup_mocks_for_rspec
- called before each example
verify_mocks_for_rspec
- called after each example if the example hasn't yet failed.
Framework should raise an exception when expectations fail
teardown_mocks_for_rspec
- called after verify_mocks_for_rspec (even if there are errors)
If the module responds to RSpec.configuration and mock_with
receives a block, it will yield the configuration object to the block e.g.
config.mock_with OtherMockFrameworkAdapter do |mod_config|
mod_config.custom_setting = true
end
# File 'rspec-core/lib/rspec/core/configuration.rb', line 793
def mock_with(framework) framework_module = if framework.is_a?(Module) framework else const_name = MOCKING_ADAPTERS.fetch(framework) do raise ArgumentError, "Unknown mocking framework: #{framework.inspect}. " \ "Pass a module or one of #{MOCKING_ADAPTERS.keys.inspect}" end RSpec::Support.require_rspec_core "mocking_adapters/#{const_name.to_s.downcase}" RSpec::Core::MockingAdapters.const_get(const_name) end new_name, old_name = [framework_module, @mock_framework].map do |mod| mod.respond_to?(:framework_name) ? mod.framework_name : :unnamed end unless new_name == old_name assert_no_example_groups_defined(:mock_framework) end if block_given? raise "#{framework_module} must respond to `configuration` so that " \ "mock_with can yield it." unless framework_module.respond_to?(:configuration) yield framework_module.configuration end @mock_framework = framework_module end
#on_example_group_definition(&block)
Invokes block before defining an example group
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2121
def on_example_group_definition(&block) on_example_group_definition_callbacks << block end
#on_example_group_definition_callbacks
Returns an array of blocks to call before defining an example group
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2127
def on_example_group_definition_callbacks @on_example_group_definition_callbacks ||= [] end
#on_existing_matching_groups(meta) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2376
def on_existing_matching_groups( ) world.traverse_example_group_trees_until do |group| (, group).tap do |applies| yield group if applies end end end
#only_failures (readonly) Also known as: #only_failures?
Indicates if the –only-failures
(or –next-failure
) flag is being used.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 199
define_reader :only_failures
#order=(value)
Sets the default global ordering strategy. By default this can be one of :defined
, :random
, but is customizable through the #register_ordering API. If order is set to ‘rand:
, the seed will also be set.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1713
delegate_to_ordering_manager :order=
#output_to_tty?(output = output_stream) ⇒ Boolean
(private)
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2314
def output_to_tty?(output=output_stream) output.respond_to?(:tty?) && output.tty? end
#output_wrapper (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2310
def output_wrapper @output_wrapper ||= OutputWrapper.new(output_stream) end
#paths_to_check(paths) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2223
def paths_to_check(paths) return paths if pattern_might_load_specs_from_vendored_dirs? paths + [Dir.getwd] end
#prepend(mod, *filters)
Tells RSpec to prepend example groups with mod
. Methods defined in mod
are exposed to examples (not example groups). Use filters
to constrain the groups in which to prepend the module.
Similar to #include, but module is included before the example group’s class in the ancestor chain.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1564
def prepend(mod, *filters) define_mixed_in_module(mod, filters, @prepend_modules, :prepend) do |group| safe_prepend(mod, group) end end
#prepend_after(scope = nil, *meta, &block)
Alias for #after.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2052
alias_method :prepend_after, :after
#prepend_before(scope = nil, *meta, &block)
Adds block
to the start of the list of #before blocks in the same scope (‘:example`, :context
, or :suite
), in contrast to #before, which adds the hook to the end of the list.
See Hooks#before for full #before hook docs.
This method differs from Hooks#prepend_before in only one way: it supports the :suite
scope. Hooks
with the :suite
scope will be run once before the first example of the entire suite is executed. Conditions passed along with :suite
are effectively ignored.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2013
def prepend_before(scope=nil, *, &block) handle_suite_hook(scope, ) do @before_suite_hooks.unshift Hooks::BeforeHook.new(block, {}) end || begin # defeat Ruby 2.5 lazy proc allocation to ensure # the methods below are passed the same proc instances # so `Hook` equality is preserved. For more info, see: # https://bugs.ruby-lang.org/issues/14045#note-5 block.__id__ add_hook_to_existing_matching_groups(, scope) { |g| g.prepend_before(scope, *, &block) } super(scope, *, &block) end end
#raise_errors_for_deprecations!
Turns deprecation warnings into errors, in order to surface the full backtrace of the call site. This can be useful when you need more context to address a deprecation than the single-line call site normally provided.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1840
def raise_errors_for_deprecations! self.deprecation_stream = Formatters::DeprecationFormatter::RaiseErrorStream.new end
#register_ordering(name) {|list| ... }
Pass the symbol :global
to set the ordering strategy that will be used to order the top-level example groups and any example groups that do not have declared :order
metadata.
Registers a named ordering strategy that can later be used to order an example group’s subgroups by adding ‘:order => <name>` metadata to the example group.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1761
delegate_to_ordering_manager :register_ordering
#reporter ⇒ RSpec::Core::Reporter
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1063
def reporter # @reporter_buffer should only ever be set in this method to cover # initialization of @reporter. @reporter_buffer || @reporter ||= begin @reporter_buffer = DeprecationReporterBuffer.new formatter_loader.prepare_default output_wrapper, deprecation_stream @reporter_buffer.play_onto(formatter_loader.reporter) @reporter_buffer = nil formatter_loader.reporter end end
#reset
# File 'rspec-core/lib/rspec/core/configuration.rb', line 608
def reset @spec_files_loaded = false reset_reporter end
#reset_filters
# File 'rspec-core/lib/rspec/core/configuration.rb', line 621
def reset_filters self.filter_manager = FilterManager.new filter_manager.include_only( Metadata.deep_hash_dup(static_config_filter_manager.inclusions.rules) ) filter_manager.exclude_only( Metadata.deep_hash_dup(static_config_filter_manager.exclusions.rules) ) end
#reset_reporter
# File 'rspec-core/lib/rspec/core/configuration.rb', line 614
def reset_reporter @reporter = nil @formatter_loader = nil @output_wrapper = nil end
#run_suite_hooks(hook_description, hooks) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2193
def run_suite_hooks(hook_description, hooks) context = SuiteHookContext.new(hook_description, reporter) hooks.each do |hook| begin hook.run(context) rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex context.set_exception(ex) # Do not run subsequent `before` hooks if one fails. # But for `after` hooks, we run them all so that all # cleanup bits get a chance to complete, minimizing the # chance that resources get left behind. break if hooks.equal?(@before_suite_hooks) end end end
#safe_extend(mod, host) (private)
See additional method definition at line 2399.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2408
def safe_extend(mod, host) host.extend(mod) unless host.singleton_class < mod end
#safe_include(mod, host) (private)
:nocov:
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2404
def safe_include(mod, host) host.__send__(:include, mod) unless host < mod end
#safe_prepend(mod, host) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2389
def safe_prepend(mod, host) host.__send__(:prepend, mod) unless host < mod end
#seed
Seed for random ordering (default: generated randomly each run).
When you run specs with ‘–order random`, ::RSpec
generates a random seed for the randomization and prints it to the #output_stream (assuming you’re using RSpec’s built-in formatters). If you discover an ordering dependency (i.e. examples fail intermittently depending on order), set this (on Configuration
or on the command line with –seed
) to run using the same seed while you debug the issue.
We recommend, actually, that you use the command line approach so you don’t accidentally leave the seed encoded.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1703
delegate_to_ordering_manager :seed
#seed=(value)
Sets the seed value and sets the default global ordering to random.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1689
delegate_to_ordering_manager :seed=
#spec_files_with_failures
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1145
def spec_files_with_failures @spec_files_with_failures ||= last_run_statuses.inject(Set.new) do |files, (id, status)| files << Example.parse_id(id).first if status == FAILED_STATUS files end.to_a end
#update_pattern_attr(name, value) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2341
def update_pattern_attr(name, value) if @spec_files_loaded RSpec.warning "Configuring `#{name}` to #{value} has no effect since " \ "RSpec has already loaded the spec files." end instance_variable_set(:"@#{name}", value) @files_to_run = nil end
#value_for(key) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/configuration.rb', line 2287
def value_for(key) @preferred_options.fetch(key) { yield } end
#when_first_matching_example_defined(*filters)
Defines a callback that runs after the first example with matching metadata is defined. If no examples are defined with matching metadata, it will not get called at all.
This can be used to ensure some setup is performed (such as bootstrapping a DB or loading a specific file that adds significantly to the boot time) if needed (as indicated by the presence of an example with matching metadata) but avoided otherwise.
# File 'rspec-core/lib/rspec/core/configuration.rb', line 1930
def when_first_matching_example_defined(*filters) = Metadata.build_hash_from(filters, :warn_about_example_group_filtering) callback = lambda do || # Example groups do not have `:example_group` metadata # (instead they have `:parent_example_group` metadata). return unless .key?(:example_group) # Ensure the callback only fires once. @derived_metadata_blocks.delete(callback, ) yield end @derived_metadata_blocks.append(callback, ) end
#windows_absolute_network_path?(pattern) ⇒ Boolean
(private)
#with_suite_hooks
# File 'rspec-core/lib/rspec/core/configuration.rb', line 2098
def with_suite_hooks return yield if dry_run? begin RSpec.current_scope = :before_suite_hook run_suite_hooks("a `before(:suite)` hook", @before_suite_hooks) yield ensure RSpec.current_scope = :after_suite_hook run_suite_hooks("an `after(:suite)` hook", @after_suite_hooks) RSpec.current_scope = :suite end end