Configuration

Read command-line configuration options from files

RSpec reads command line configuration options from files in three different
locations:

  • Local: ./.rspec-local (i.e. in the project’s root directory, can be
    gitignored)
  • Project: ./.rspec (i.e. in the project’s root directory, usually
    checked into the project)
  • Global: ~/.rspec (i.e. in the user’s home directory)

Configuration options are loaded from ~/.rspec.rspec.rspec-local,
command line switches, and the SPEC_OPTS environment variable (listed in
lowest to highest precedence; for example, an option in ~/.rspec can be
overridden by an option in .rspec-local).Scenarios

  1. Color set in `.rspec`
  2. Custom options file
  3. RSpec ignores `./.rspec` when custom options file is used
  4. Using ERB in `.rspec`

Color set in `.rspec`Givena file named “.rspec” with:

--color

Anda file named “spec/example_spec.rb” with:

RSpec.describe "color_enabled?" do
  context "when set with RSpec.configure" do
    before do
      # color is disabled for non-tty output, so stub the output stream
      # to say it is tty, even though we're running this with cucumber
      allow(RSpec.configuration.output_stream).to receive(:tty?) { true }
    end

    it "is true" do
      expect(RSpec.configuration).to be_color_enabled
    end
  end
end

WhenI run rspec ./spec/example_spec.rbThenthe examples should all passCustom options fileGivena file named “my.options” with:

--format documentation

Anda file named “spec/example_spec.rb” with:

RSpec.describe "formatter set in custom options file" do
  it "sets formatter" do
    expect(RSpec.configuration.formatters.first).
      to be_a(RSpec::Core::Formatters::DocumentationFormatter)
  end
end

WhenI run rspec spec/example_spec.rb --options my.optionsThenthe examples should all passRSpec ignores `./.rspec` when custom options file is usedGivena file named “my.options” with:

--format documentation

Anda file named “.rspec” with:

--color

Anda file named “spec/example_spec.rb” with:

RSpec.describe "custom options file" do
  it "causes .rspec to be ignored" do
    expect(RSpec.configuration.color).to be_falsey
  end
end

WhenI run rspec spec/example_spec.rb --options my.optionsThenthe examples should all passUsing ERB in `.rspec`Givena file named “.rspec” with:

--format <%= true ? 'documentation' : 'progress' %>

Anda file named “spec/example_spec.rb” with:

RSpec.describe "formatter" do
  it "is set to documentation" do
    expect(RSpec.configuration.formatters.first).
      to be_an(RSpec::Core::Formatters::DocumentationFormatter)
  end
end

WhenI run rspec ./spec/example_spec.rbThenthe examples should all pass

Windows may require additional solutions to display color

The output uses ANSI escape codes to show text in color. Windows systems (shells) often don’t interpret those codes at all. If you’re on Windows and you see ANSI escape codes in the output (something like [1m [31m ) and your text isn’t in different colors, you may need to install a utility so that your Windows shell will interpret those codes correctly and show the colors. Here are some popular solutions:

  • ANSICON: ANSICON runs ‘on top of’ cmd or powershell. This is a very
    popular solution. You can set it up so that it’s always used whenever
    you use cmd or powershell, or use it only at specific times.
  • Alternatives to cmd.exe or powershell: ConEmuConsole2ConsoleZ
  • Unix-like shells and utilities: cygwinbabunMinGW (Minimalist GNU for Windows)

To find out more, search for information about those solutions.

Fail fast

RSpec.configure {|c| c.fail_fast = 1}

Use the fail_fast option to tell RSpec to abort the run after N failures

In RSpec, the --fail-fast option stops the test suite on the first failure. When this option is enabled, RSpec will immediately stop running any additional tests once a test failure is encountered, rather than continuing to run all the remaining tests in the suite.

This can be useful in situations where you want to quickly identify and debug a specific test failure, without wasting time running additional tests that are likely to also fail due to the same issue. However, it can also be a hindrance in cases where there are multiple failures, as it may prevent you from seeing all the issues at once.

To enable the --fail-fast option, you can simply pass it as a command-line argument when running your RSpec tests, like so:

rspec --fail-fast

Alternatively, you can set it as a default option in your RSpec configuration by adding the following line to your spec_helper.rb file:

RSpec.configure do |config|
  config.fail_fast = true
end

Custom settings

Extensions like rspec-rails can add their own configuration settings

Simple setting (with defaults)

additional_setting_spec.rb

RSpec.configure do |c|
  c.add_setting :custom_setting
end

RSpec.describe "custom setting" do
  it "is nil by default" do
    expect(RSpec.configuration.custom_setting).to be_nil
  end

  it "acts false by default" do
    expect(RSpec.configuration.custom_setting).to be_falsey
  end

  it "is exposed as a predicate" do
    expect(RSpec.configuration.custom_setting?).to be_falsey
  end

  it "can be overridden" do
    RSpec.configuration.custom_setting = true
    expect(RSpec.configuration.custom_setting).to be_truthy
    expect(RSpec.configuration.custom_setting?).to be_truthy
  end
end

Default to `true`

additional_setting_spec.rb

RSpec.configure do |c|
  c.add_setting :custom_setting, :default => true
end

RSpec.describe "custom setting" do
  it "is true by default" do
    expect(RSpec.configuration.custom_setting).to be_truthy
  end

  it "is exposed as a predicate" do
    expect(RSpec.configuration.custom_setting?).to be_truthy
  end

  it "can be overridden" do
    RSpec.configuration.custom_setting = false
    expect(RSpec.configuration.custom_setting).to be_falsey
    expect(RSpec.configuration.custom_setting?).to be_falsey
  end
end

Overridden in a subsequent `RSpec.configure` block

additional_setting_spec.rb

RSpec.configure do |c|
  c.add_setting :custom_setting
end

RSpec.configure do |c|
  c.custom_setting = true
end

RSpec.describe "custom setting" do
  it "returns the value set in the last cofigure block to get eval'd" do
    expect(RSpec.configuration.custom_setting).to be_truthy
  end

  it "is exposed as a predicate" do
    expect(RSpec.configuration.custom_setting?).to be_truthy
  end
end

Create example aliases

Use config.alias_example_to to create new example group methods that define
examples with the configured metadata. You can also specify metadata using
only symbols.

Use `alias_example_to` to define a custom example name

alias_example_to_spec.rb

RSpec.configure do |c|
  c.alias_example_to :task
end

RSpec.describe "a task example group" do
  task "does something" do
    expect(5).to eq(5)
  end
end

rspec alias_example_to_spec.rb --format doc

Use `alias_example_to` to define a pending example

alias_example_to_pending_spec.rb

RSpec.configure do |c|
  c.alias_example_to :pit, :pending => "Pit alias used"
end

RSpec.describe "an example group" do
  pit "does something later on" do
    fail "not implemented yet"
  end
end

rspec alias_example_to_pending_spec.rb --format doc

“does something later on (PENDING: Pit alias used)”

Use symbols as metadata

use_symbols_as_metadata_spec.rb

RSpec.configure do |c|
  c.alias_example_to :pit, :pending
end

RSpec.describe "an example group" do
  pit "does something later on" do
    fail "not implemented yet"
  end
end

rspec use_symbols_as_metadata_spec.rb --format doc

Setting the default spec path

You can type rspec to run all specs that live in the spec directory.

This is supported by a --default-path option, which is set to spec by default. If you prefer to keep your specs in a different directory, or assign an individual file to --default-path, you can do so on the command line or in a configuration file (.rspec~/.rspec, or a custom file).

NOTE: this option is not supported on RSpec.configuration, as it needs to be set before spec files are loaded.

Run `rspec` with default `default-path` (`spec` directory)

spec/example_spec.rb

RSpec.describe "an example" do
  it "passes" do
  end
end

WhenI run rspec

“1 example, 0 failures”

Run `rspec` with customized `default-path`

Set this in your .rspec file. (Notice the dot).

--default-path behavior

behavior/example_spec.rb

RSpec.describe "an example" do
  it "passes" do
  end
end

“1 example, 0 failures”

Custom deprecation stream

Define a custom output stream for warning about deprecations (default $stderr).

RSpec.configure do |c|
  c.deprecation_stream = File.open('deprecations.txt', 'w')
end

or

RSpec.configure { |c| c.deprecation_stream = 'deprecations.txt' }

or pass --deprecation-outBackgroundGivena file named “lib/foo.rb” with:

class Foo
  def bar
    RSpec.deprecate "Foo#bar"
  end
end

Default – print deprecations to `$stderr`

spec/example_spec.rb

require "foo"

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

“Deprecation Warnings:\n\nFoo#bar is deprecated”

Configure using the path to a file

Gspec/example_spec.rb

require "foo"

RSpec.configure {|c| c.deprecation_stream = 'deprecations.txt' }

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

rspec spec/example_spec.rb

“Deprecation Warnings:”

“1 deprecation logged to deprecations.txt”

“deprecations.txt” gets written to:

“Foo#bar is deprecated

Configure using a `File` object

spec/example_spec.rb

require "foo"

RSpec.configure do |c|
  c.deprecation_stream = File.open('deprecations.txt', 'w')
end

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

configure using the CLI `–deprecation-out` option

spec/example_spec.rb

require "foo"
RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

rspec spec/example_spec.rb --deprecation-out deprecations.txt

"Deprecation Warnings:"
"1 deprecation logged to deprecations.txt"

deprecations.txt will get written to:

 "Foo#bar is deprecated"

Custom output stream

Define a custom output stream (default $stdout). Aliases: :output, :out.

RSpec.configure { |c| c.output_stream = File.open('saved_output', 'w') }

spec/spec_helper.rb

RSpec.configure { |c| c.output_stream = File.open('saved_output', 'w') }

Scenarios

Redirecting output

spec/example_spec.rb

require 'spec_helper'
RSpec.describe "an example" do
  it "passes" do
    true
  end
end

rspec spec/example_spec.rb

saved_output has the content

1 example, 0 failures

Exclude_pattern

Use the --exclude-pattern option to tell RSpec to skip looking for specs in files
that match the pattern specified.

spec/models/model_spec.rb

RSpec.describe "two specs here" do
  it "passes" do
  end

  it "passes too" do
  end
end

Anda file named “spec/features/feature_spec.rb” with:

RSpec.describe "only one spec" do
  it "passes" do
  end
end

Scenarios

  1. By default, RSpec runs files that match `”**/*_spec.rb”`
  2. The `–exclude-pattern` flag makes RSpec skip matching files
  3. The `–exclude-pattern` flag can be used to pass in multiple patterns, separated by comma
  4. The `–exclude-pattern` flag accepts shell style glob unions
  5. The `–exclude-pattern` flag can be used with the `–pattern` flag

By default, RSpec runs files that match `”**/*_spec.rb”`

rspec

“3 examples, 0 failures”

The `–exclude-pattern` flag makes RSpec skip matching files

rspec --exclude-pattern "**/models/*_spec.rb"

“1 example, 0 failures”

The `–exclude-pattern` flag can be used to pass in multiple patterns, separated by comma

rspec --exclude-pattern "**/models/*_spec.rb, **/features/*_spec.rb"

“0 examples, 0 failures”

The `–exclude-pattern` flag accepts shell style glob unions

rspec --exclude-pattern "**/{models,features}/*_spec.rb"

“0 examples, 0 failures”

The `–exclude-pattern` flag can be used with the `–pattern` flag

rspec --pattern "spec/**/*_spec.rb" --exclude-pattern "spec/models/*_spec.rb"

“1 example, 0 failures”

Failure exit code

Use the failure_exit_code option to set a custom exit code when RSpec fails.

RSpec.configure { |c| c.failure_exit_code = 42 }

spec/spec_helper.rb

RSpec.configure { |c| c.failure_exit_code = 42 }

A failing spec with the default exit code

spec/example_spec.rb

RSpec.describe "something" do
  it "fails" do
    fail
  end
end

rspec spec/example_spec.rb

spec/example_spec.rb

require 'spec_helper'
RSpec.describe "something" do
  it "fails" do
    fail
  end
end

This will exit status with a status of 42

Exit with the default exit code when an `at_exit` hook is added upstream

exit_at_spec.rb

require 'rspec/autorun'
at_exit { exit(0) }

RSpec.describe "exit 0 at_exit ignored" do
  it "does not interfere with the default exit code" do
    fail
  end
end

the exit status will be 1

Excluding lines from the backtrace

To reduce the noise when diagnosing failures, RSpec can exclude lines belonging to certain gems or matching given patterns.

If you want to filter out backtrace lines belonging to specific gems, you can use config.filter_gems_from_backtrace like so:

config.filter_gems_from_backtrace "ignored_gem", "another_ignored_gem",

For more control over which lines to ignore, you can use the backtrace_exclusion_patterns option to either replace the default exclusion patterns or append your own, e.g.

config.backtrace_exclusion_patterns = [/first pattern/, /second pattern/]
config.backtrace_exclusion_patterns << /another pattern/

The default exclusion patterns are:

/\/lib\d*\/ruby\//,
/org\/jruby\//,
/bin\//,
/lib\/rspec\/(core|expectations|matchers|mocks)/

Additionally, rspec can be run with the --backtrace option to skip backtrace cleaning entirely.Scenarios

Using default `backtrace_exclusion_patterns`

spec/failing_spec.rb

RSpec.describe "2 + 2" do
  it "is 5" do
    expect(2+2).to eq(5)
  end
end

“1 example, 1 failure”

Replacing `backtrace_exclusion_patterns`

spec/spec_helper.rb

RSpec.configure do |config|
  config.backtrace_exclusion_patterns = [
    /spec_helper/
  ]
end

spec/example_spec.rb

require 'spec_helper'
RSpec.describe "foo" do
  it "returns baz" do
    expect("foo").to eq("baz")
  end
end

1 example, 1 failure

“lib/rspec/expectations”

Appending to `backtrace_exclusion_patterns`

spec/support/assert_baz.rb

require "support/really_assert_baz"

def assert_baz(arg)
  really_assert_baz(arg)
end

spec/support/really_assert_baz.rb

def really_assert_baz(arg)
  expect(arg).to eq("baz")
end

spec/example_spec.rb

require "support/assert_baz"
RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /really/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

“1 example, 1 failure”

“assert_baz”

Running `rspec` with `–backtrace` prints unfiltered backtraces

spec/support/custom_helper.rb

def assert_baz(arg)
  expect(arg).to eq("baz")
end

Anda file named “spec/example_spec.rb” with:

require "support/custom_helper"

RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /custom_helper/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

WhenI run rspec --backtrace

“1 example, 1 failure”

“spec/support/custom_helper.rb:2:in `assert_baz'”

“lib/rspec/expectations”

“lib/rspec/core”

Using `filter_gems_from_backtrace` to filter the named gem

assume a gem “my_gem” has a file lib/my_gem.rb

class MyGem
  def self.do_amazing_things!
    # intentional bug to trigger an exception
    impossible_math = 10 / 0
    "10 div 0 is: #{impossible_math}"
  end
end

spec/use_my_gem_spec.rb

require 'my_gem'

RSpec.describe "Using my_gem" do
  it 'does amazing things' do
    expect(MyGem.do_amazing_things!).to include("10 div 0 is")
  end
end

Anda file named “spec/spec_helper.rb” with:

RSpec.configure do |config|
  config.filter_gems_from_backtrace "my_gem"
end

vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'”

rspec --require spec_helper 

output does not contain “vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'”

Failure exit code

Use the failure_exit_code option to set a custom exit code when RSpec fails.

RSpec.configure { |c| c.failure_exit_code = 42 }

spec/spec_helper.rb

RSpec.configure { |c| c.failure_exit_code = 42 }

A failing spec with the default exit code

spec/example_spec.rb

RSpec.describe "something" do
  it "fails" do
    fail
  end
end

WhenI run rspec spec/example_spec.rb

Then the exit status should be 1

A failing spec with a custom exit code

spec/example_spec.rb

require 'spec_helper'
RSpec.describe "something" do
  it "fails" do
    fail
  end
end

rspec spec/example_spec.rb

exit status will be 42

Exit with the default exit code when an `at_exit` hook is added upstream

exit_at_spec.rb

require 'rspec/autorun'
at_exit { exit(0) }

RSpec.describe "exit 0 at_exit ignored" do
  it "does not interfere with the default exit code" do
    fail
  end
end

exit status will be 1

Global namespace DSL

RSpec has a few top-level constructs that allow you to begin describing
behavior:

  • RSpec.describe: Define a named context for a group of examples.
  • RSpec.shared_examples Define a set of shared examples that can be included in an example group later
  • RSpec.shared_context: define some common context (using beforelet,
    helper methods, etc) that can later be included in an example group.

Historically, these constructs have been available directly off of the main object, so that you could use these at the start of a file without the RSpec. prefix. They have also been available off of any class or module so that you can scope your examples within a particular constant namespace.

RSpec 3 now provides an option to disable this global monkey patching:

config.expose_dsl_globally = false

Overriding global ordering

You can customize how RSpec orders examples and example groups. For an individual group, you can control it by tagging it with :order metadata:

  • :defined runs the examples (and sub groups) in defined order
  • :random runs them in random order

If you have more specialized needs, you can register your own ordering using the register_ordering configuration option. If you register an ordering as :global, it will be the global default, used by all groups that do not have :order metadata (and by RSpec to order the top-level groups).

Running a specific example group in order

RSpec.describe "examples only pass when they are run in order", :order => :defined do
  before(:context) { @list = [] }

  it "passes when run first" do
    @list << 1
    expect(@list).to eq([1])
  end

  it "passes when run second" do
    @list << 2
    expect(@list).to eq([1, 2])
  end

  it "passes when run third" do
    @list << 3
    expect(@list).to eq([1, 2, 3])
  end
end

rspec order_dependent_spec.rb --order random:1

Registering a custom ordering

RSpec.configure do |rspec|
  rspec.register_ordering(:reverse) do |items|
    items.reverse
  end
end

RSpec.describe "A group that must run in reverse order", :order => :reverse do
  before(:context) { @list = [] }

  it "passes when run second" do
    @list << 2
    expect(@list).to eq([1, 2])
  end

  it "passes when run first" do
    @list << 1
    expect(@list).to eq([1])
  end
end

Using a custom global ordering

RSpec.configure do |rspec|
  rspec.register_ordering(:global) do |items|
    items.reverse
  end
end

RSpec.describe "A group without :order metadata" do
  before(:context) { @list = [] }

  it "passes when run second" do
    @list << 2
    expect(@list).to eq([1, 2])
  end

  it "passes when run first" do
    @list << 1
    expect(@list).to eq([1])
  end
end

.rspec file

Rather than using require 'spec_helper' at the top of each spec file, ensure that you have --require spec_helper in .rspec. That will always load before the pattern is resolved. With the pattern thus configured, only those spec files that match the pattern will then be loaded.

Pattern

Use the pattern option to configure RSpec to look for specs in files. The default pattern is **/*_spec.rb

Override the default pattern in configuration

If, for example, you’d rather have your spec files with an extension .spec, you’d use this:

RSpec.configure { |c| c.pattern = '**/*.spec' }

This lets you run files that end in a .spec extension like spec/one_example.spec

Append to the default pattern in configuration

RSpec.configure do |config|
  config.pattern << ',**/*.spec'
end

This will run the default pattern and also files that have a file extension .spec

Profile examples

The --profile command line option (available from RSpec.configure as #profile_examples), when set, will cause RSpec to dump out a list of your slowest examples. By default, it prints the 10 slowest examples, but you can set it to a different value to have it print more or fewer slow examples.

If --fail-fast option is used together with --profile and there is a failure, slow examples are not shown.

spec/example_spec.rb

require "spec_helper"

RSpec.describe "something" do
  it "sleeps for 0.1 seconds (example 1)" do
    sleep 0.1
    expect(1).to eq(1)
  end

  it "sleeps for 0 seconds (example 2)" do
    expect(2).to eq(2)
  end

  it "sleeps for 0.15 seconds (example 3)" do
    sleep 0.15
    expect(3).to eq(3)
  end

  it "sleeps for 0.05 seconds (example 4)" do
    sleep 0.05
    expect(4).to eq(4)
  end

  it "sleeps for 0.05 seconds (example 5)" do
    sleep 0.05
    expect(5).to eq(5)
  end

  it "sleeps for 0.05 seconds (example 6)" do
    sleep 0.05
    expect(6).to eq(6)
  end

  it "sleeps for 0.05 seconds (example 7)" do
    sleep 0.05
    expect(7).to eq(7)
  end

  it "sleeps for 0.05 seconds (example 8)" do
    sleep 0.05
    expect(8).to eq(8)
  end

  it "sleeps for 0.05 seconds (example 9)" do
    sleep 0.05
    expect(9).to eq(9)
  end

  it "sleeps for 0.05 seconds (example 10)" do
    sleep 0.05
    expect(10).to eq(10)
  end

  it "sleeps for 0.05 seconds (example 11)" do
    sleep 0.05
    expect(11).to eq(11)
  end
end

By default does not show profile

The output will contain the 10 slowest examples

Setting `profile_examples` to true shows 10 examples

spec/spec_helper.rb

RSpec.configure { |c| c.profile_examples = true }

The examples should all pass and show the 10 slowest examples.

Setting `profile_examples` to 2 shows 2 slowest examples

spec/spec_helper.rb

RSpec.configure { |c| c.profile_examples = 2 }

WhenI run rspec spec

Thenthe examples should all pass

Andthe output shows you the top 2 slowest examples

Setting profile examples through CLI using `–profile`

rspec spec --profile 2

Thenthe examples should all pass

the output shows the top 2 slowest examples

Using `–no-profile` overrules config options

spec/spec_helper.rb

RSpec.configure { |c| c.profile_examples = true }

rspec spec --no-profile

The examples will run and pass, but no profiling will be performed, even though you have it configured in spec_helper.rb.

Using `–profile` shows slow examples even in case of failures

spec/example_spec.rb

require "spec_helper"

RSpec.describe "something" do
  it "sleeps for 0.1 seconds (example 1)" do
    sleep 0.1
    expect(1).to eq(1)
  end

  it "fails" do
    fail
  end
end

rspec spec --profile

“2 examples, 1 failure”

“Top 2 slowest examples”

Using `–profile` with `–fail-fast` doesn’t show slow examples in case of failures

spec/example_spec.rb

require "spec_helper"

RSpec.describe "something" do
  it "sleeps for 0.1 seconds (example 1)" do
    sleep 0.1
    expect(1).to eq(1)
  end

  it "fails" do
    fail
  end
end

rspec spec --fail-fast --profile

Here, the slow (profiled) examples are not shown because --fail-fast is set.

Using `–profile` with slow before hooks includes hook execution time

spec/example_spec.rb

RSpec.describe "slow before context hook" do
  before(:context) do
    sleep 0.2
  end

  context "nested" do
    it "example" do
      expect(10).to eq(10)
    end
  end
end

RSpec.describe "slow example" do
  it "slow example" do
    sleep 0.1
    expect(10).to eq(10)
  end
end

rspec spec --profile 1

This example demonstrates that the examples are measured including whatever setup happens in a before hook.

Run all when everything filtered

Use the run_all_when_everything_filtered option to tell RSpec to run all the specs in the case where you try to run a filtered list of specs but no specs match that filter. This works well when paired with an inclusion filter like focus: true, as it will run all the examples when none match the
inclusion filter.

RSpec.configure { |c| c.run_all_when_everything_filtered = true }

In RSpec, the run_all_when_everything_filtered option is used to automatically run all examples in the test suite when a filtering condition is applied that includes all the examples.

By default, when you run RSpec with a filter, it only runs the examples that match the filter. For example, if you have a test suite with 100 examples and you run rspec spec --tag foo, it will only run the examples that have been tagged with :foo.

However, if you enable the run_all_when_everything_filtered option, when you apply a filter that includes all examples, RSpec will automatically run all examples in the test suite. For example, if you have a test suite with 100 examples and you run rspec spec --tag ~foo, it will run all 100 examples, even though none of them have been tagged with :foo.

To enable this option, you can add the following line to your RSpec configuration:

RSpec.configure do |config|
  config.run_all_when_everything_filtered = true
end

This can be useful in situations where you want to run the entire test suite, but also want to be able to filter it down to specific examples or groups of examples for debugging or other purposes.

When the `run_all_when_everything_filtered` option is turned on, if there are any matches for the filtering tag, only those features are run

spec/example_spec.rb

require "spec_helper"
RSpec.describe "examples" do
  it "with no tag", :some_tag => true do
  end

  it "with no tag as well" do
  end
end

rspec spec/example_spec.rb --tag some_tag

“1 example, 0 failures”

“Run options: include {:some_tag=>true}”

When the `run_all_when_everything_filtered` option is turned on, all the specs are run when the tag has no matches

spec/example_spec.rb

require "spec_helper"
RSpec.describe "examples" do
  it "with no tag" do
  end

  it "with no tag as well" do
  end
end

rspec spec/example_spec.rb --tag some_tag

“2 examples, 0 failures”

“All examples were filtered out; ignoring {:some_tag=>true}”

Set the order and/or seed

You can set the order to run specs and specify a seed if you are running the specs using the ‘random’ ordering.

See the documentation on the --order command line option for more on this.Scenarios

Zero monkey patching mode

Use the disable_monkey_patching! configuration option to disable all monkey patching done by RSpec:

  • stops exposing DSL globally
  • disables should and should_not syntax for rspec-expectations
  • disables stubshould_receive, and should_not_receive syntax for rspec-mocks
RSpec.configure { |c| c.disable_monkey_patching! }

spec/example_describe_spec.rb

require 'spec_helper'

describe "specs here" do
  it "passes" do
  end
end

spec/example_should_spec.rb

require 'spec_helper'

RSpec.describe "another specs here" do
  it "passes with monkey patched expectations" do
    x = 25
    x.should eq 25
    x.should_not be > 30
  end

  it "passes with monkey patched mocks" do
    x = double("thing")
    x.stub(:hello => [:world])
    x.should_receive(:count).and_return(4)
    x.should_not_receive(:all)
    (x.hello * x.count).should eq([:world, :world, :world, :world])
  end
end

spec/example_expect_spec.rb

require 'spec_helper'

RSpec.describe "specs here too" do
  it "passes in zero monkey patching mode" do
    x = double("thing")
    allow(x).to receive(:hello).and_return([:world])
    expect(x).to receive(:count).and_return(4)
    expect(x).not_to receive(:all)
    expect(x.hello * x.count).to eq([:world, :world, :world, :world])
  end

  it "passes in zero monkey patching mode" do
    x = 25
    expect(x).to eq(25)
    expect(x).not_to be > 30
  end
end

By default RSpec allows monkey patching

spec/spec_helper.rb

# Empty spec_helper

WhenI run rspec

Thenthe examples should all pass

Monkey patched methods are undefined with `disable_monkey_patching!`

spec/spec_helper.rb

RSpec.configure do |config|
  config.disable_monkey_patching!
end

rspec spec/example_should_spec.rb

undefined method `should'
unexpected message :stub

rspec spec/example_describe_spec.rb

output should contain “undefined method `describe'”

`allow` and `expect` syntax works with monkey patching

spec/spec_helper.rb

# Empty spec_helper

`allow` and `expect` syntax works without monkey patching

spec/spec_helper.rb

RSpec.configure do |config|
  config.disable_monkey_patching!
end

rspec spec/example_expect_spec.rb