Module: RSpec

Defined in:
lib/rspec/core.rb,
lib/rspec/core/drb.rb,
lib/rspec/core/dsl.rb,
lib/rspec/core/set.rb,
lib/rspec/core/hooks.rb,
lib/rspec/core/world.rb,
lib/rspec/core/runner.rb,
lib/rspec/core/example.rb,
lib/rspec/core/pending.rb,
lib/rspec/core/sandbox.rb,
lib/rspec/core/version.rb,
lib/rspec/core/flat_map.rb,
lib/rspec/core/metadata.rb,
lib/rspec/core/ordering.rb,
lib/rspec/core/profiler.rb,
lib/rspec/core/warnings.rb,
lib/rspec/core/rake_task.rb,
lib/rspec/core/invocations.rb,
lib/rspec/core/did_you_mean.rb,
lib/rspec/core/ruby_project.rb,
lib/rspec/core/shell_escape.rb,
lib/rspec/core/bisect/server.rb,
lib/rspec/core/configuration.rb,
lib/rspec/core/example_group.rb,
lib/rspec/core/filter_manager.rb,
lib/rspec/core/output_wrapper.rb,
lib/rspec/core/shared_context.rb,
lib/rspec/core/metadata_filter.rb,
lib/rspec/core/bisect/utilities.rb,
lib/rspec/core/memoized_helpers.rb,
lib/rspec/core/bisect/coordinator.rb,
lib/rspec/core/bisect/fork_runner.rb,
lib/rspec/core/formatters/helpers.rb,
lib/rspec/core/backtrace_formatter.rb,
lib/rspec/core/bisect/shell_runner.rb,
lib/rspec/core/formatters/protocol.rb,
lib/rspec/core/mocking_adapters/rr.rb,
lib/rspec/core/project_initializer.rb,
lib/rspec/core/bisect/shell_command.rb,
lib/rspec/core/shared_example_group.rb,
lib/rspec/core/configuration_options.rb,
lib/rspec/core/mocking_adapters/null.rb,
lib/rspec/core/mocking_adapters/mocha.rb,
lib/rspec/core/mocking_adapters/rspec.rb,
lib/rspec/core/formatters/html_printer.rb,
lib/rspec/core/bisect/example_minimizer.rb,
lib/rspec/core/example_status_persister.rb,
lib/rspec/core/formatters/console_codes.rb,
lib/rspec/core/formatters/base_formatter.rb,
lib/rspec/core/formatters/html_formatter.rb,
lib/rspec/core/formatters/json_formatter.rb,
lib/rspec/core/mocking_adapters/flexmock.rb,
lib/rspec/core/minitest_assertions_adapter.rb,
lib/rspec/core/formatters/profile_formatter.rb,
lib/rspec/core/formatters/snippet_extractor.rb,
lib/rspec/core/test_unit_assertions_adapter.rb,
lib/rspec/core/formatters/progress_formatter.rb,
lib/rspec/core/formatters/syntax_highlighter.rb,
lib/rspec/core/formatters/base_text_formatter.rb,
lib/rspec/core/formatters/exception_presenter.rb,
lib/rspec/core/formatters/bisect_drb_formatter.rb,
lib/rspec/core/formatters/base_bisect_formatter.rb,
lib/rspec/core/formatters/deprecation_formatter.rb,
lib/rspec/core/formatters/failure_list_formatter.rb,
lib/rspec/core/formatters/html_snippet_extractor.rb,
lib/rspec/core/formatters/documentation_formatter.rb,
lib/rspec/core/formatters/bisect_progress_formatter.rb,
lib/rspec/core/formatters/fallback_message_formatter.rb

Overview

This is borrowed (slightly modified) from Scott Taylor's project_path project: http://github.com/smtlaissezfaire/project_path

Defined Under Namespace

Modules: Core

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationvoid

Returns the global Configuration object. While you can use this method to access the configuration, the more common convention is to use RSpec.configure.

Examples:

RSpec.configuration.drb_port = 1234

See Also:



85
86
87
# File 'lib/rspec/core.rb', line 85

def self.configuration
  @configuration ||= RSpec::Core::Configuration.new
end

.world=(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Setters for shared global objects



49
50
51
# File 'lib/rspec/core.rb', line 49

def world=(value)
  @world = value
end

Class Method Details

.clear_examplesvoid

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.



70
71
72
73
74
75
# File '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| ... } ⇒ void

Yields the global configuration to a block.

Examples:

RSpec.configure do |config|
  config.add_formatter 'documentation'
end

Yields:

  • (Configuration)

    global configuration

See Also:



97
98
99
# File 'lib/rspec/core.rb', line 97

def self.configure
  yield configuration if block_given?
end

.current_examplevoid

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.

Examples:


RSpec.configure do |c|
  # context.example is deprecated, but RSpec.current_example is not
  # available until RSpec 3.0.
  fetch_current_example = RSpec.respond_to?(:current_example) ?
    proc { RSpec.current_example } : proc { |context| context.example }

  c.before(:example) do
    example = fetch_current_example.call(self)

    # ...
  end
end


122
123
124
# File 'lib/rspec/core.rb', line 122

def self.current_example
  RSpec::Support.thread_local_data[:current_example]
end

.current_example=(example) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the current example being executed.



128
129
130
# File 'lib/rspec/core.rb', line 128

def self.current_example=(example)
  RSpec::Support.thread_local_data[:current_example] = example
end

.current_scopeSymbol

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.

Returns:

  • (Symbol)


154
155
156
# File 'lib/rspec/core.rb', line 154

def self.current_scope
  RSpec::Support.thread_local_data[:current_scope]
end

.current_scope=(scope) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the current scope rspec is executing in



134
135
136
# File 'lib/rspec/core.rb', line 134

def self.current_scope=(scope)
  RSpec::Support.thread_local_data[:current_scope] = scope
end

.resetvoid

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.



58
59
60
61
62
# File 'lib/rspec/core.rb', line 58

def self.reset
  RSpec::ExampleGroups.remove_all_constants
  @world = nil
  @configuration = nil
end