////
  Do NOT edit this file by hand directly, as it is automatically generated.

  Please make any necessary changes to the cop documentation within the source files themselves.
////

= RSpec

[#rspecalignleftletbrace]
== RSpec/AlignLeftLetBrace

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| Always
| 1.16
| -
|===

Checks that left braces for adjacent single line lets are aligned.

[#examples-rspecalignleftletbrace]
=== Examples

[source,ruby]
----
# bad
let(:foobar) { blahblah }
let(:baz) { bar }
let(:a) { b }

# good
let(:foobar) { blahblah }
let(:baz)    { bar }
let(:a)      { b }
----

[#references-rspecalignleftletbrace]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/AlignLeftLetBrace

[#rspecalignrightletbrace]
== RSpec/AlignRightLetBrace

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| Always
| 1.16
| -
|===

Checks that right braces for adjacent single line lets are aligned.

[#examples-rspecalignrightletbrace]
=== Examples

[source,ruby]
----
# bad
let(:foobar) { blahblah }
let(:baz)    { bar }
let(:a)      { b }

# good
let(:foobar) { blahblah }
let(:baz)    { bar      }
let(:a)      { b        }
----

[#references-rspecalignrightletbrace]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/AlignRightLetBrace

[#rspecanyinstance]
== RSpec/AnyInstance

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.4
| -
|===

Check that instances are not being stubbed globally.

Prefer instance doubles over stubbing any instance of a class

[#examples-rspecanyinstance]
=== Examples

[source,ruby]
----
# bad
describe MyClass do
  before { allow_any_instance_of(MyClass).to receive(:foo) }
end

# good
describe MyClass do
  let(:my_instance) { instance_double(MyClass) }

  before do
    allow(MyClass).to receive(:new).and_return(my_instance)
    allow(my_instance).to receive(:foo)
  end
end
----

[#references-rspecanyinstance]
=== References

* https://rspec.rubystyle.guide/#any_instance_of
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/AnyInstance

[#rspecaroundblock]
== RSpec/AroundBlock

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.11
| -
|===

Checks that around blocks actually run the test.

[#examples-rspecaroundblock]
=== Examples

[source,ruby]
----
# bad
around do
  some_method
end

around do |test|
  some_method
end

# good
around do |test|
  some_method
  test.call
end

around do |test|
  some_method
  test.run
end
----

[#references-rspecaroundblock]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/AroundBlock

[#rspecbe]
== RSpec/Be

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.25
| -
|===

Check for expectations where `be` is used without argument.

The `be` matcher is too generic, as it pass on everything that is not
nil or false. If that is the exact intend, use `be_truthy`. In all other
cases it's better to specify what exactly is the expected value.

[#examples-rspecbe]
=== Examples

[source,ruby]
----
# bad
expect(foo).to be

# good
expect(foo).to be_truthy
expect(foo).to be 1.0
expect(foo).to be(true)
----

[#references-rspecbe]
=== References

* https://rspec.rubystyle.guide/#be-matcher
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Be

[#rspecbeempty]
== RSpec/BeEmpty

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 2.20
| 2.31
|===

Prefer using `be_empty` when checking for an empty array.

[#examples-rspecbeempty]
=== Examples

[source,ruby]
----
# bad
expect(array).to contain_exactly
expect(array).to match_array([])

# good
expect(array).to be_empty
----

[#references-rspecbeempty]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEmpty

[#rspecbeeq]
== RSpec/BeEq

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| No
| Always (Unsafe)
| 2.9.0
| 2.16
|===

Check for expectations where `be(...)` can replace `eq(...)`.

The `be` matcher compares by identity while the `eq` matcher compares
using `==`. Booleans and nil can be compared by identity and therefore
the `be` matcher is preferable as it is a more strict test.

[#safety-rspecbeeq]
=== Safety

This cop is unsafe because it changes how values are compared.

[#examples-rspecbeeq]
=== Examples

[source,ruby]
----
# bad
expect(foo).to eq(true)
expect(foo).to eq(false)
expect(foo).to eq(nil)

# good
expect(foo).to be(true)
expect(foo).to be(false)
expect(foo).to be(nil)
----

[#references-rspecbeeq]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEq

[#rspecbeeql]
== RSpec/BeEql

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| No
| Always (Unsafe)
| 1.7
| 2.16
|===

Check for expectations where `be(...)` can replace `eql(...)`.

The `be` matcher compares by identity while the `eql` matcher
compares using `eql?`. Integers, floats, booleans, symbols, and nil
can be compared by identity and therefore the `be` matcher is
preferable as it is a more strict test.

This cop only looks for instances of `expect(...).to eql(...)`. We
do not check `to_not` or `not_to` since `!eql?` is more strict
than `!equal?`. We also do not try to flag `eq` because if
`a == b`, and `b` is comparable by identity, `a` is still not
necessarily the same type as `b` since the `#==` operator can
coerce objects for comparison.

[#safety-rspecbeeql]
=== Safety

This cop is unsafe because it changes how values are compared.

[#examples-rspecbeeql]
=== Examples

[source,ruby]
----
# bad
expect(foo).to eql(1)
expect(foo).to eql(1.0)
expect(foo).to eql(true)
expect(foo).to eql(false)
expect(foo).to eql(:bar)
expect(foo).to eql(nil)

# good
expect(foo).to be(1)
expect(foo).to be(1.0)
expect(foo).to be(true)
expect(foo).to be(false)
expect(foo).to be(:bar)
expect(foo).to be(nil)
----

[#references-rspecbeeql]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEql

[#rspecbenil]
== RSpec/BeNil

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.9.0
| 2.10.0
|===

Ensures a consistent style is used when matching `nil`.

You can either use the more specific `be_nil` matcher, or the more
generic `be` matcher with a `nil` argument.

This cop can be configured using the `EnforcedStyle` option

[#examples-rspecbenil]
=== Examples

[#_enforcedstyle_-be_nil_-_default_-rspecbenil]
==== `EnforcedStyle: be_nil` (default)

[source,ruby]
----
# bad
expect(foo).to be(nil)

# good
expect(foo).to be_nil
----

[#_enforcedstyle_-be_-rspecbenil]
==== `EnforcedStyle: be`

[source,ruby]
----
# bad
expect(foo).to be_nil

# good
expect(foo).to be(nil)
----

[#configurable-attributes-rspecbenil]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `be_nil`
| `be`, `be_nil`
|===

[#references-rspecbenil]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeNil

[#rspecbeforeafterall]
== RSpec/BeforeAfterAll

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.12
| 2.23
|===

Check that before/after(:all/:context) isn't being used.

[#examples-rspecbeforeafterall]
=== Examples

[source,ruby]
----
# bad - Faster but risk of state leaking between examples
describe MyClass do
  before(:all) { Widget.create }
  after(:context) { Widget.delete_all }
end

# good - Slower but examples are properly isolated
describe MyClass do
  before(:each) { Widget.create }
  after(:each) { Widget.delete_all }
end
----

[#configurable-attributes-rspecbeforeafterall]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Exclude
| `+**/spec/spec_helper.rb+`, `+**/spec/rails_helper.rb+`, `+**/spec/support/**/*.rb+`
| Array
|===

[#references-rspecbeforeafterall]
=== References

* https://rspec.rubystyle.guide/#avoid-hooks-with-context-scope
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll

[#rspecchangebyzero]
== RSpec/ChangeByZero

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.11
| 2.14
|===

Prefer negated matchers over `to change.by(0)`.

In the case of composite expectations, cop suggest using the
negation matchers of `RSpec::Matchers#change`.

By default the cop does not support autocorrect of
compound expectations, but if you set the
negated matcher for `change`, e.g. `not_change` with
the `NegatedMatcher` option, the cop will perform the autocorrection.

[#examples-rspecchangebyzero]
=== Examples

[#negatedmatcher_-_-_default_-rspecchangebyzero]
==== NegatedMatcher: ~ (default)

[source,ruby]
----
# bad
expect { run }.to change(Foo, :bar).by(0)
expect { run }.to change { Foo.bar }.by(0)

# bad - compound expectations (does not support autocorrection)
expect { run }
  .to change(Foo, :bar).by(0)
  .and change(Foo, :baz).by(0)
expect { run }
  .to change { Foo.bar }.by(0)
  .and change { Foo.baz }.by(0)

# good
expect { run }.not_to change(Foo, :bar)
expect { run }.not_to change { Foo.bar }

# good - compound expectations
define_negated_matcher :not_change, :change
expect { run }
  .to not_change(Foo, :bar)
  .and not_change(Foo, :baz)
expect { run }
  .to not_change { Foo.bar }
  .and not_change { Foo.baz }
----

[#negatedmatcher_-not_change-rspecchangebyzero]
==== NegatedMatcher: not_change

[source,ruby]
----
# bad (support autocorrection to good case)
expect { run }
  .to change(Foo, :bar).by(0)
  .and change(Foo, :baz).by(0)
expect { run }
  .to change { Foo.bar }.by(0)
  .and change { Foo.baz }.by(0)

# good
define_negated_matcher :not_change, :change
expect { run }
  .to not_change(Foo, :bar)
  .and not_change(Foo, :baz)
expect { run }
  .to not_change { Foo.bar }
  .and not_change { Foo.baz }
----

[#configurable-attributes-rspecchangebyzero]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| NegatedMatcher
| `<none>`
| 
|===

[#references-rspecchangebyzero]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ChangeByZero

[#rspecclasscheck]
== RSpec/ClassCheck

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.13
| -
|===

Enforces consistent use of `be_a` or `be_kind_of`.

[#examples-rspecclasscheck]
=== Examples

[#enforcedstyle_-be_a-_default_-rspecclasscheck]
==== EnforcedStyle: be_a (default)

[source,ruby]
----
# bad
expect(object).to be_kind_of(String)
expect(object).to be_a_kind_of(String)

# good
expect(object).to be_a(String)
expect(object).to be_an(String)
----

[#enforcedstyle_-be_kind_of-rspecclasscheck]
==== EnforcedStyle: be_kind_of

[source,ruby]
----
# bad
expect(object).to be_a(String)
expect(object).to be_an(String)

# good
expect(object).to be_kind_of(String)
expect(object).to be_a_kind_of(String)
----

[#configurable-attributes-rspecclasscheck]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `be_a`
| `be_a`, `be_kind_of`
|===

[#references-rspecclasscheck]
=== References

* https://rubystyle.guide#is-a-vs-kind-of
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ClassCheck

[#rspeccontainexactly]
== RSpec/ContainExactly

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.19
| -
|===

Checks where `contain_exactly` is used.

This cop checks for the following:

- Prefer `match_array` when matching array values.
- Prefer `be_empty` when using `contain_exactly` with no arguments.

[#examples-rspeccontainexactly]
=== Examples

[source,ruby]
----
# bad
it { is_expected.to contain_exactly(*array1, *array2) }

# good
it { is_expected.to match_array(array1 + array2) }

# good
it { is_expected.to contain_exactly(content, *array) }
----

[#references-rspeccontainexactly]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContainExactly

[#rspeccontextmethod]
== RSpec/ContextMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.36
| -
|===

`context` should not be used for specifying methods.

[#examples-rspeccontextmethod]
=== Examples

[source,ruby]
----
# bad
context '#foo_bar' do
  # ...
end

context '.foo_bar' do
  # ...
end

# good
describe '#foo_bar' do
  # ...
end

describe '.foo_bar' do
  # ...
end
----

[#references-rspeccontextmethod]
=== References

* https://rspec.rubystyle.guide/#example-group-naming
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextMethod

[#rspeccontextwording]
== RSpec/ContextWording

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.20
| 2.13
|===

Checks that `context` docstring starts with an allowed prefix.

The default list of prefixes is minimal. Users are encouraged to tailor
the configuration to meet project needs. Other acceptable prefixes may
include `if`, `unless`, `for`, `before`, `after`, or `during`.
They may consist of multiple words if desired.

If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
report an offense. So you need to set at least one of them.

This cop can be customized allowed context description pattern
with `AllowedPatterns`. By default, there are no checking by pattern.

[#examples-rspeccontextwording]
=== Examples

[#_prefixes_-configuration-rspeccontextwording]
==== `Prefixes` configuration

[source,ruby]
----
# .rubocop.yml
# RSpec/ContextWording:
#   Prefixes:
#     - when
#     - with
#     - without
#     - if
#     - unless
#     - for
----

[source,ruby]
----
# bad
context 'the display name not present' do
  # ...
end

# good
context 'when the display name is not present' do
  # ...
end
----

[#_allowedpatterns_-configuration-rspeccontextwording]
==== `AllowedPatterns` configuration

[source,ruby]
----
# .rubocop.yml
# RSpec/ContextWording:
#   AllowedPatterns:
#     - とき$
----

[source,ruby]
----
# bad
context '条件を満たす' do
  # ...
end

# good
context '条件を満たすとき' do
  # ...
end
----

[#configurable-attributes-rspeccontextwording]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Prefixes
| `when`, `with`, `without`
| Array

| AllowedPatterns
| `[]`
| Array
|===

[#references-rspeccontextwording]
=== References

* https://rspec.rubystyle.guide/#context-descriptions
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextWording
* http://www.betterspecs.org/#contexts

[#rspecdescribeclass]
== RSpec/DescribeClass

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.0
| 2.7
|===

Check that the first argument to the top-level describe is a constant.

It can be configured to ignore strings when certain metadata is passed.

Ignores Rails and Aruba `type` metadata by default.

[#examples-rspecdescribeclass]
=== Examples

[#_ignoredmetadata_-configuration-rspecdescribeclass]
==== `IgnoredMetadata` configuration

[source,ruby]
----
# .rubocop.yml
# RSpec/DescribeClass:
#   IgnoredMetadata:
#     type:
#       - request
#       - controller
----

[source,ruby]
----
# bad
describe 'Do something' do
end

# good
describe TestedClass do
  subject { described_class }
end

describe 'TestedClass::VERSION' do
  subject { Object.const_get(self.class.description) }
end

describe "A feature example", type: :feature do
end
----

[#configurable-attributes-rspecdescribeclass]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Exclude
| `+**/spec/features/**/*+`, `+**/spec/requests/**/*+`, `+**/spec/routing/**/*+`, `+**/spec/system/**/*+`, `+**/spec/views/**/*+`
| Array

| IgnoredMetadata
| `{"type" => ["channel", "controller", "helper", "job", "mailer", "model", "request", "routing", "view", "feature", "system", "mailbox", "aruba", "task"]}`
| 
|===

[#references-rspecdescribeclass]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeClass

[#rspecdescribemethod]
== RSpec/DescribeMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.0
| -
|===

Checks that the second argument to `describe` specifies a method.

[#examples-rspecdescribemethod]
=== Examples

[source,ruby]
----
# bad
describe MyClass, 'do something' do
end

# good
describe MyClass, '#my_instance_method' do
end

describe MyClass, '.my_class_method' do
end
----

[#references-rspecdescribemethod]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeMethod

[#rspecdescribesymbol]
== RSpec/DescribeSymbol

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.15
| -
|===

Avoid describing symbols.

[#examples-rspecdescribesymbol]
=== Examples

[source,ruby]
----
# bad
describe :my_method do
  # ...
end

# good
describe '#my_method' do
  # ...
end
----

[#references-rspecdescribesymbol]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeSymbol
* https://github.com/rspec/rspec-core/issues/1610

[#rspecdescribedclass]
== RSpec/DescribedClass

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always (Unsafe)
| 1.0
| 2.27
|===

Checks that tests use `described_class`.

If the first argument of describe is a class, the class is exposed to
each example via described_class.

This cop can be configured using the `EnforcedStyle`, `SkipBlocks`
and `OnlyStaticConstants` options.
`OnlyStaticConstants` is only relevant when `EnforcedStyle` is
`described_class`.

There's a known caveat with rspec-rails's `controller` helper that
runs its block in a different context, and `described_class` is not
available to it. `SkipBlocks` option excludes detection in all
non-RSpec related blocks.

To narrow down this setting to only a specific directory, it is
possible to use an overriding configuration file local to that
directory.

[#safety-rspecdescribedclass]
=== Safety

Autocorrection is unsafe when `SkipBlocks: false` because
`described_class` might not be available within the block (for
example, in rspec-rails's `controller` helper).

[#examples-rspecdescribedclass]
=== Examples

[#_enforcedstyle_-described_class_-_default_-rspecdescribedclass]
==== `EnforcedStyle: described_class` (default)

[source,ruby]
----
# bad
describe MyClass do
  subject { MyClass.do_something }
end

# good
describe MyClass do
  subject { described_class.do_something }
end
----

[#_onlystaticconstants_-true_-_default_-rspecdescribedclass]
==== `OnlyStaticConstants: true` (default)

[source,ruby]
----
# good
describe MyClass do
  subject { MyClass::CONSTANT }
end
----

[#_onlystaticconstants_-false_-rspecdescribedclass]
==== `OnlyStaticConstants: false`

[source,ruby]
----
# bad
describe MyClass do
  subject { MyClass::CONSTANT }
end
----

[#_enforcedstyle_-explicit_-rspecdescribedclass]
==== `EnforcedStyle: explicit`

[source,ruby]
----
# bad
describe MyClass do
  subject { described_class.do_something }
end

# good
describe MyClass do
  subject { MyClass.do_something }
end
----

[#_skipblocks_-true_-rspecdescribedclass]
==== `SkipBlocks: true`

[source,ruby]
----
# spec/controllers/.rubocop.yml
# RSpec/DescribedClass:
#   SkipBlocks: true

# acceptable
describe MyConcern do
  controller(ApplicationController) do
    include MyConcern
  end
end
----

[#configurable-attributes-rspecdescribedclass]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| SkipBlocks
| `false`
| Boolean

| EnforcedStyle
| `described_class`
| `described_class`, `explicit`

| OnlyStaticConstants
| `true`
| Boolean
|===

[#references-rspecdescribedclass]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribedClass

[#rspecdescribedclassmodulewrapping]
== RSpec/DescribedClassModuleWrapping

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| No
| 1.37
| -
|===

Avoid opening modules and defining specs within them.

[#examples-rspecdescribedclassmodulewrapping]
=== Examples

[source,ruby]
----
# bad
module MyModule
  RSpec.describe MyClass do
    # ...
  end
end

# good
RSpec.describe MyModule::MyClass do
  # ...
end
----

[#references-rspecdescribedclassmodulewrapping]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribedClassModuleWrapping
* https://github.com/rubocop/rubocop-rspec/issues/735

[#rspecdialect]
== RSpec/Dialect

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| Always
| 1.33
| -
|===

Enforces custom RSpec dialects.

A dialect can be based on the following RSpec methods:

- describe, context, feature, example_group
- xdescribe, xcontext, xfeature
- fdescribe, fcontext, ffeature
- shared_examples, shared_examples_for, shared_context
- it, specify, example, scenario, its
- fit, fspecify, fexample, fscenario, focus
- xit, xspecify, xexample, xscenario, skip
- pending
- prepend_before, before, append_before,
- around
- prepend_after, after, append_after
- let, let!
- subject, subject!
- expect, is_expected, expect_any_instance_of
- raise_error, raise_exception

By default all of the RSpec methods and aliases are allowed. By setting
a config like:

  RSpec/Dialect:
    PreferredMethods:
      context: describe

If you were previously using the `RSpec/Capybara/FeatureMethods` cop and
want to keep disabling all Capybara-specific methods that have the same
native RSpec method (e.g. are just aliases), use the following config:

  RSpec/Dialect:
    PreferredMethods:
      background: :before
      scenario:   :it
      xscenario:  :xit
      given:      :let
      given!:     :let!
      feature:    :describe

You can expect the following behavior:

[#examples-rspecdialect]
=== Examples

[source,ruby]
----
# bad
context 'display name presence' do
  # ...
end

# good
describe 'display name presence' do
  # ...
end
----

[#configurable-attributes-rspecdialect]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| PreferredMethods
| `{}`
| 
|===

[#references-rspecdialect]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Dialect

[#rspecduplicatedmetadata]
== RSpec/DuplicatedMetadata

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.16
| -
|===

Avoid duplicated metadata.

[#examples-rspecduplicatedmetadata]
=== Examples

[source,ruby]
----
# bad
describe 'Something', :a, :a

# good
describe 'Something', :a
----

[#references-rspecduplicatedmetadata]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DuplicatedMetadata

[#rspecemptyexamplegroup]
== RSpec/EmptyExampleGroup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only (Unsafe)
| 1.7
| 2.31
|===

Checks if an example group does not include any tests.

[#examples-rspecemptyexamplegroup]
=== Examples

[#usage-rspecemptyexamplegroup]
==== usage

[source,ruby]
----
# bad
describe Bacon do
  let(:bacon)      { Bacon.new(chunkiness) }
  let(:chunkiness) { false                 }

  context 'extra chunky' do   # flagged by rubocop
    let(:chunkiness) { true }
  end

  it 'is chunky' do
    expect(bacon.chunky?).to be_truthy
  end
end

# good
describe Bacon do
  let(:bacon)      { Bacon.new(chunkiness) }
  let(:chunkiness) { false                 }

  it 'is chunky' do
    expect(bacon.chunky?).to be_truthy
  end
end

# good
describe Bacon do
  pending 'will add tests later'
end
----

[#references-rspecemptyexamplegroup]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup

[#rspecemptyhook]
== RSpec/EmptyHook

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.39
| 2.31
|===

Checks for empty before and after hooks.

[#examples-rspecemptyhook]
=== Examples

[source,ruby]
----
# bad
before {}
after do; end
before(:all) do
end
after(:all) { }

# good
before { create_users }
after do
  cleanup_users
end
before(:all) do
  create_feed
end
after(:all) { cleanup_feed }
----

[#references-rspecemptyhook]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyHook

[#rspecemptylineafterexample]
== RSpec/EmptyLineAfterExample

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.36
| -
|===

Checks if there is an empty line after example blocks.

[#examples-rspecemptylineafterexample]
=== Examples

[source,ruby]
----
# bad
RSpec.describe Foo do
  it 'does this' do
  end
  it 'does that' do
  end
end

# good
RSpec.describe Foo do
  it 'does this' do
  end

  it 'does that' do
  end
end

# fair - it's ok to have non-separated one-liners
RSpec.describe Foo do
  it { one }
  it { two }
end
----

[#with-allowconsecutiveoneliners-configuration-rspecemptylineafterexample]
==== with AllowConsecutiveOneLiners configuration

[source,ruby]
----
# rubocop.yml
# RSpec/EmptyLineAfterExample:
#   AllowConsecutiveOneLiners: false

# bad
RSpec.describe Foo do
  it { one }
  it { two }
end
----

[#configurable-attributes-rspecemptylineafterexample]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowConsecutiveOneLiners
| `true`
| Boolean
|===

[#references-rspecemptylineafterexample]
=== References

* https://rspec.rubystyle.guide/#empty-lines-around-examples
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterExample

[#rspecemptylineafterexamplegroup]
== RSpec/EmptyLineAfterExampleGroup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.27
| -
|===

Checks if there is an empty line after example group blocks.

[#examples-rspecemptylineafterexamplegroup]
=== Examples

[source,ruby]
----
# bad
RSpec.describe Foo do
  describe '#bar' do
  end
  describe '#baz' do
  end
end

# good
RSpec.describe Foo do
  describe '#bar' do
  end

  describe '#baz' do
  end
end
----

[#references-rspecemptylineafterexamplegroup]
=== References

* https://rspec.rubystyle.guide/#empty-lines-between-describes
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterExampleGroup

[#rspecemptylineafterfinallet]
== RSpec/EmptyLineAfterFinalLet

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.14
| -
|===

Checks if there is an empty line after the last let block.

[#examples-rspecemptylineafterfinallet]
=== Examples

[source,ruby]
----
# bad
let(:foo) { bar }
let(:something) { other }
it { does_something }

# good
let(:foo) { bar }
let(:something) { other }

it { does_something }
----

[#references-rspecemptylineafterfinallet]
=== References

* https://rspec.rubystyle.guide/#empty-line-after-let
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterFinalLet

[#rspecemptylineafterhook]
== RSpec/EmptyLineAfterHook

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.27
| 2.13
|===

Checks if there is an empty line after hook blocks.

`AllowConsecutiveOneLiners` configures whether adjacent
one-line definitions are considered an offense.

[#examples-rspecemptylineafterhook]
=== Examples

[source,ruby]
----
# bad
before { do_something }
it { does_something }

# bad
after { do_something }
it { does_something }

# bad
around { |test| test.run }
it { does_something }

# good
after { do_something }

it { does_something }

# fair - it's ok to have non-separated one-liners hooks
around { |test| test.run }
after { do_something }

it { does_something }
----

[#with-allowconsecutiveoneliners-configuration-rspecemptylineafterhook]
==== with AllowConsecutiveOneLiners configuration

[source,ruby]
----
# rubocop.yml
# RSpec/EmptyLineAfterHook:
#   AllowConsecutiveOneLiners: false

# bad
around { |test| test.run }
after { do_something }

it { does_something }

# good
around { |test| test.run }

after { do_something }

it { does_something }
----

[#configurable-attributes-rspecemptylineafterhook]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowConsecutiveOneLiners
| `true`
| Boolean
|===

[#references-rspecemptylineafterhook]
=== References

* https://rspec.rubystyle.guide/#empty-line-after-let
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterHook

[#rspecemptylineaftersubject]
== RSpec/EmptyLineAfterSubject

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.14
| -
|===

Checks if there is an empty line after subject block.

[#examples-rspecemptylineaftersubject]
=== Examples

[source,ruby]
----
# bad
subject(:obj) { described_class }
let(:foo) { bar }

# good
subject(:obj) { described_class }

let(:foo) { bar }
----

[#references-rspecemptylineaftersubject]
=== References

* https://rspec.rubystyle.guide/#empty-line-after-let
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterSubject

[#rspecemptymetadata]
== RSpec/EmptyMetadata

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 2.24
| 2.31
|===

Avoid empty metadata hash.

[#examples-rspecemptymetadata]
=== Examples

[#enforcedstyle_-symbol-_default_-rspecemptymetadata]
==== EnforcedStyle: symbol (default)

[source,ruby]
----
# bad
describe 'Something', {}

# good
describe 'Something'
----

[#references-rspecemptymetadata]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyMetadata

[#rspecemptyoutput]
== RSpec/EmptyOutput

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.29
| -
|===

Check that the `output` matcher is not called with an empty string.

[#examples-rspecemptyoutput]
=== Examples

[source,ruby]
----
# bad
expect { foo }.to output('').to_stdout
expect { bar }.not_to output('').to_stderr

# good
expect { foo }.not_to output.to_stdout
expect { bar }.to output.to_stderr
----

[#references-rspecemptyoutput]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyOutput

[#rspeceq]
== RSpec/Eq

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.24
| -
|===

Use `eq` instead of `be ==` to compare objects.

[#examples-rspeceq]
=== Examples

[source,ruby]
----
# bad
expect(foo).to be == 42

# good
expect(foo).to eq 42
----

[#references-rspeceq]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Eq

[#rspecexamplelength]
== RSpec/ExampleLength

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.5
| 2.3
|===

Checks for long examples.

A long example is usually more difficult to understand. Consider
extracting out some behavior, e.g. with a `let` block, or a helper
method.

You can set constructs you want to fold with `CountAsOne`.
Available are: 'array', 'hash', 'heredoc', and 'method_call'.
Each construct will be counted as one line regardless of
its actual size.

[#examples-rspecexamplelength]
=== Examples

[source,ruby]
----
# bad
it do
  service = described_class.new
  more_setup
  more_setup
  result = service.call
  expect(result).to be(true)
end

# good
it do
  service = described_class.new
  result = service.call
  expect(result).to be(true)
end
----

[#countasone_-__array__-_heredoc__-_method_call__-rspecexamplelength]
==== CountAsOne: ['array', 'heredoc', 'method_call']

[source,ruby]
----
it do
  array = [         # +1
    1,
    2
  ]

  hash = {          # +3
    key: 'value'
  }

  msg = <<~HEREDOC  # +1
    Heredoc
    content.
  HEREDOC

  foo(            # +1
    1,
    2
  )
end               # 6 points
----

[#configurable-attributes-rspecexamplelength]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Max
| `5`
| Integer

| CountAsOne
| `[]`
| Array
|===

[#references-rspecexamplelength]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleLength

[#rspecexamplewithoutdescription]
== RSpec/ExampleWithoutDescription

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.22
| -
|===

Checks for examples without a description.

RSpec allows for auto-generated example descriptions when there is no
description provided or the description is an empty one.
It is acceptable to use `specify` without a description

This cop removes empty descriptions.
It also defines whether auto-generated description is allowed, based
on the configured style.

This cop can be configured using the `EnforcedStyle` option

[#examples-rspecexamplewithoutdescription]
=== Examples

[source,ruby]
----
# always good
specify do
  result = service.call
  expect(result).to be(true)
end
----

[#_enforcedstyle_-always_allow_-_default_-rspecexamplewithoutdescription]
==== `EnforcedStyle: always_allow` (default)

[source,ruby]
----
# bad
it('') { is_expected.to be_good }
specify '' do
  result = service.call
  expect(result).to be(true)
end

# good
it { is_expected.to be_good }
specify do
  result = service.call
  expect(result).to be(true)
end
----

[#_enforcedstyle_-single_line_only_-rspecexamplewithoutdescription]
==== `EnforcedStyle: single_line_only`

[source,ruby]
----
# bad
it('') { is_expected.to be_good }
it do
  result = service.call
  expect(result).to be(true)
end

# good
it { is_expected.to be_good }
----

[#_enforcedstyle_-disallow_-rspecexamplewithoutdescription]
==== `EnforcedStyle: disallow`

[source,ruby]
----
# bad
it { is_expected.to be_good }
it do
  result = service.call
  expect(result).to be(true)
end
----

[#configurable-attributes-rspecexamplewithoutdescription]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `always_allow`
| `always_allow`, `single_line_only`, `disallow`
|===

[#references-rspecexamplewithoutdescription]
=== References

* https://rspec.rubystyle.guide/#it-and-specify
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWithoutDescription

[#rspecexamplewording]
== RSpec/ExampleWording

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.0
| 2.13
|===

Checks for common mistakes in example descriptions.

This cop will correct docstrings that begin with 'should' and 'it'.
This cop will also look for insufficient examples and call them out.

Use the DisallowedExamples setting to prevent unclear or insufficient
descriptions. Please note that this config will not be treated as
case sensitive.

[#safety-rspecexamplewording]
=== Safety

The autocorrect is experimental - use with care! It can be configured
with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).

While the autocorrect will not break your code (it only modifies test
description strings, not the actual test logic), it may produce
grammatically incorrect English in some cases. Always review the diff
when using autocorrect to ensure the descriptions remain natural and
accurate.

This is not classified as an unsafe autocorrect because it does not
affect code behavior, but manual review of changes is strongly
recommended.

[#examples-rspecexamplewording]
=== Examples

[source,ruby]
----
# bad
it 'should find nothing' do
end

it 'will find nothing' do
end

# good
it 'finds nothing' do
end
----

[source,ruby]
----
# bad
it 'it does things' do
end

# good
it 'does things' do
end
----

[#_disallowedexamples_-__works___-_default_-rspecexamplewording]
==== `DisallowedExamples: ['works']` (default)

[source,ruby]
----
# bad
it 'works' do
end

# good
it 'marks the task as done' do
end
----

[#configurable-attributes-rspecexamplewording]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| CustomTransform
| `{"be" => "is", "BE" => "IS", "have" => "has", "HAVE" => "HAS"}`
| 

| IgnoredWords
| `[]`
| Array

| DisallowedExamples
| `works`
| Array
|===

[#references-rspecexamplewording]
=== References

* https://rspec.rubystyle.guide/#should-in-example-docstrings
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording
* http://betterspecs.org/#should

[#rspecexcessivedocstringspacing]
== RSpec/ExcessiveDocstringSpacing

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.5
| -
|===

Checks for excessive whitespace in example descriptions.

[#examples-rspecexcessivedocstringspacing]
=== Examples

[source,ruby]
----
# bad
it '  has  excessive   spacing  ' do
end

# good
it 'has excessive spacing' do
end
----

[source,ruby]
----
# bad
context '  when a condition   is met  ' do
end

# good
context 'when a condition is met' do
end
----

[#references-rspecexcessivedocstringspacing]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExcessiveDocstringSpacing

[#rspecexpectactual]
== RSpec/ExpectActual

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.7
| 2.23
|===

Checks for `expect(...)` calls containing literal values.

Autocorrection is performed when the expected is not a literal.

[#examples-rspecexpectactual]
=== Examples

[source,ruby]
----
# bad
expect(5).to eq(price)
expect(/foo/).to eq(pattern)
expect("John").to eq(name)

# good
expect(price).to eq(5)
expect(pattern).to eq(/foo/)
expect(name).to eq("John")

# bad (not supported autocorrection)
expect(false).to eq(true)
----

[#configurable-attributes-rspecexpectactual]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Exclude
| `+**/spec/routing/**/*+`
| Array
|===

[#references-rspecexpectactual]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectActual

[#rspecexpectchange]
== RSpec/ExpectChange

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always (Unsafe)
| 1.22
| 2.5
|===

Checks for consistent style of change matcher.

Enforces either passing a receiver and message as method arguments,
or a block.

This cop can be configured using the `EnforcedStyle` option.

[#safety-rspecexpectchange]
=== Safety

Autocorrection is unsafe because `method_call` style calls the
receiver *once* and sends the message to it before and after
calling the `expect` block, whereas `block` style calls the
expression *twice*, including the receiver.

If your receiver is dynamic (e.g., the result of a method call) and
you expect it to be called before and after the `expect` block,
changing from `block` to `method_call` style may break your test.

[source,ruby]
----
expect { run }.to change { my_method.message }
# `my_method` is called before and after `run`

expect { run }.to change(my_method, :message)
# `my_method` is called once, but `message` is called on it twice
----

[#examples-rspecexpectchange]
=== Examples

[#_enforcedstyle_-method_call_-_default_-rspecexpectchange]
==== `EnforcedStyle: method_call` (default)

[source,ruby]
----
# bad
expect { run }.to change { Foo.bar }
expect { run }.to change { foo.baz }

# good
expect { run }.to change(Foo, :bar)
expect { run }.to change(foo, :baz)
# also good when there are arguments or chained method calls
expect { run }.to change { Foo.bar(:count) }
expect { run }.to change { user.reload.name }
----

[#_enforcedstyle_-block_-rspecexpectchange]
==== `EnforcedStyle: block`

[source,ruby]
----
# bad
expect { run }.to change(Foo, :bar)

# good
expect { run }.to change { Foo.bar }
----

[#configurable-attributes-rspecexpectchange]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `method_call`
| `method_call`, `block`
|===

[#references-rspecexpectchange]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectChange

[#rspecexpectinhook]
== RSpec/ExpectInHook

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.16
| -
|===

Do not use `expect` in hooks such as `before`.

[#examples-rspecexpectinhook]
=== Examples

[source,ruby]
----
# bad
before do
  expect(something).to eq 'foo'
end

# bad
after do
  expect_any_instance_of(Something).to receive(:foo)
end

# good
it do
  expect(something).to eq 'foo'
end
----

[#references-rspecexpectinhook]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectInHook

[#rspecexpectinlet]
== RSpec/ExpectInLet

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.30
| -
|===

Do not use `expect` in let.

[#examples-rspecexpectinlet]
=== Examples

[source,ruby]
----
# bad
let(:foo) do
  expect(something).to eq 'foo'
end

# good
it do
  expect(something).to eq 'foo'
end
----

[#references-rspecexpectinlet]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectInLet

[#rspecexpectoutput]
== RSpec/ExpectOutput

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.10
| -
|===

Checks for opportunities to use `expect { ... }.to output`.

[#examples-rspecexpectoutput]
=== Examples

[source,ruby]
----
# bad
$stdout = StringIO.new
my_app.print_report
$stdout = STDOUT
expect($stdout.string).to eq('Hello World')

# good
expect { my_app.print_report }.to output('Hello World').to_stdout
----

[#references-rspecexpectoutput]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectOutput

[#rspecfocus]
== RSpec/Focus

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.5
| 2.31
|===

Checks if examples are focused.

This cop does not support autocorrection in some cases.

[#examples-rspecfocus]
=== Examples

[source,ruby]
----
# bad
describe MyClass, focus: true do
end

describe MyClass, :focus do
end

fdescribe MyClass do
end

# good
describe MyClass do
end

# bad
fdescribe 'test' do; end

# good
describe 'test' do; end

# bad
shared_examples 'test', focus: true do; end

# good
shared_examples 'test' do; end

# bad
shared_context 'test', focus: true do; end

# good
shared_context 'test' do; end

# bad (does not support autocorrection)
focus 'test' do; end
----

[#references-rspecfocus]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Focus

[#rspechookargument]
== RSpec/HookArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.7
| -
|===

Checks the arguments passed to `before`, `around`, and `after`.

This cop checks for consistent style when specifying RSpec
hooks which run for each example. There are three supported
styles: "implicit", "each", and "example." All styles have
the same behavior.

[#examples-rspechookargument]
=== Examples

[#_enforcedstyle_-implicit_-_default_-rspechookargument]
==== `EnforcedStyle: implicit` (default)

[source,ruby]
----
# bad
before(:each) do
  # ...
end

# bad
before(:example) do
  # ...
end

# good
before do
  # ...
end
----

[#_enforcedstyle_-each_-rspechookargument]
==== `EnforcedStyle: each`

[source,ruby]
----
# bad
before(:example) do
  # ...
end

# bad
before do
  # ...
end

# good
before(:each) do
  # ...
end
----

[#_enforcedstyle_-example_-rspechookargument]
==== `EnforcedStyle: example`

[source,ruby]
----
# bad
before(:each) do
  # ...
end

# bad
before do
  # ...
end

# good
before(:example) do
  # ...
end
----

[#configurable-attributes-rspechookargument]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `implicit`
| `implicit`, `each`, `example`
|===

[#references-rspechookargument]
=== References

* https://rspec.rubystyle.guide/#redundant-beforeeach
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HookArgument

[#rspechooksbeforeexamples]
== RSpec/HooksBeforeExamples

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.29
| 2.31
|===

Checks for before/around/after hooks that come after an example.

[#examples-rspechooksbeforeexamples]
=== Examples

[source,ruby]
----
# bad
it 'checks what foo does' do
  expect(foo).to be
end

before { prepare }
after { clean_up }

# good
before { prepare }
after { clean_up }

it 'checks what foo does' do
  expect(foo).to be
end
----

[#references-rspechooksbeforeexamples]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples

[#rspecidenticalequalityassertion]
== RSpec/IdenticalEqualityAssertion

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.4
| -
|===

Checks for equality assertions with identical expressions on both sides.

[#examples-rspecidenticalequalityassertion]
=== Examples

[source,ruby]
----
# bad
expect(foo.bar).to eq(foo.bar)
expect(foo.bar).to eql(foo.bar)

# good
expect(foo.bar).to eq(2)
expect(foo.bar).to eql(2)
----

[#references-rspecidenticalequalityassertion]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion

[#rspecimplicitblockexpectation]
== RSpec/ImplicitBlockExpectation

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.35
| -
|===

Check that implicit block expectation syntax is not used.

Prefer using explicit block expectations.

[#examples-rspecimplicitblockexpectation]
=== Examples

[source,ruby]
----
# bad
subject { -> { do_something } }
it { is_expected.to change(something).to(new_value) }

# good
it 'changes something to a new value' do
  expect { do_something }.to change(something).to(new_value)
end
----

[#references-rspecimplicitblockexpectation]
=== References

* https://rspec.rubystyle.guide/#implicit-block-expectations
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ImplicitBlockExpectation

[#rspecimplicitexpect]
== RSpec/ImplicitExpect

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.8
| -
|===

Check that a consistent implicit expectation style is used.

This cop can be configured using the `EnforcedStyle` option
and supports the `--auto-gen-config` flag.

[#examples-rspecimplicitexpect]
=== Examples

[#_enforcedstyle_-is_expected_-_default_-rspecimplicitexpect]
==== `EnforcedStyle: is_expected` (default)

[source,ruby]
----
# bad
it { should be_truthy }

# good
it { is_expected.to be_truthy }
----

[#_enforcedstyle_-should_-rspecimplicitexpect]
==== `EnforcedStyle: should`

[source,ruby]
----
# bad
it { is_expected.to be_truthy }

# good
it { should be_truthy }
----

[#configurable-attributes-rspecimplicitexpect]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `is_expected`
| `is_expected`, `should`
|===

[#references-rspecimplicitexpect]
=== References

* https://rspec.rubystyle.guide/#use-expect
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ImplicitExpect

[#rspecimplicitsubject]
== RSpec/ImplicitSubject

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.29
| 2.13
|===

Checks for usage of implicit subject (`is_expected` / `should`).

This cop can be configured using the `EnforcedStyle` option

[#examples-rspecimplicitsubject]
=== Examples

[#_enforcedstyle_-single_line_only_-_default_-rspecimplicitsubject]
==== `EnforcedStyle: single_line_only` (default)

[source,ruby]
----
# bad
it do
  is_expected.to be_truthy
end

# good
it { is_expected.to be_truthy }
it do
  expect(subject).to be_truthy
end
----

[#_enforcedstyle_-single_statement_only_-rspecimplicitsubject]
==== `EnforcedStyle: single_statement_only`

[source,ruby]
----
# bad
it do
  foo = 1
  is_expected.to be_truthy
end

# good
it do
  foo = 1
  expect(subject).to be_truthy
end
it do
  is_expected.to be_truthy
end
----

[#_enforcedstyle_-disallow_-rspecimplicitsubject]
==== `EnforcedStyle: disallow`

[source,ruby]
----
# bad
it { is_expected.to be_truthy }

# good
it { expect(subject).to be_truthy }
----

[#_enforcedstyle_-require_implicit_-rspecimplicitsubject]
==== `EnforcedStyle: require_implicit`

[source,ruby]
----
# bad
it { expect(subject).to be_truthy }

# good
it { is_expected.to be_truthy }

# bad
it do
  expect(subject).to be_truthy
end

# good
it do
  is_expected.to be_truthy
end

# good
it { expect(named_subject).to be_truthy }
----

[#configurable-attributes-rspecimplicitsubject]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `single_line_only`
| `single_line_only`, `single_statement_only`, `disallow`, `require_implicit`
|===

[#references-rspecimplicitsubject]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ImplicitSubject

[#rspecincludeexamples]
== RSpec/IncludeExamples

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always (Unsafe)
| 3.6
| 3.7
|===

Checks for usage of `include_examples`.

`include_examples`, unlike `it_behaves_like`, does not create its
own context. As such, using `subject`, `let`, `before`/`after`, etc.
within shared examples included with `include_examples` can have
unexpected behavior and side effects.

Prefer using `it_behaves_like` instead.

[#safety-rspecincludeexamples]
=== Safety

`include_examples` and `it_behaves_like` have different scoping
behaviors.
Changing `include_examples` to `it_behaves_like` creates a new
context, altering setup dependencies, which can lead to unexpected
test failures.
Specifically, the scope of hooks (`before`, `after`, `around`)
changes, which may prevent expected setup from being inherited
correctly.

Additionally, `let` and `subject` are affected by scoping rules.
When `include_examples` is used, `let` and `subject` defined within
`shared_examples` are evaluated in the caller's context, allowing
access to their values.
In contrast, `it_behaves_like` creates a new context, preventing
access to `let` or `subject` values from the caller's context.

[source,ruby]
----
shared_examples "mock behavior" do
  before do
    allow(service).to receive(:call).and_return("mocked response")
  end

  it "returns mocked response" do
    expect(service.call).to eq "mocked response"
  end
end

context "working example with include_examples" do
  let(:service) { double(:service) }

  include_examples "mock behavior"

  it "uses the mocked service" do
    expect(service.call).to eq "mocked response" # Passes
  end
end

context "broken example with it_behaves_like" do
  let(:service) { double(:service) }

  it_behaves_like "mock behavior"

  it "unexpectedly does not use the mocked service" do
    # Fails because `it_behaves_like` does not apply the mock setup
    expect(service.call).to eq "mocked response"
  end
end
----

[#examples-rspecincludeexamples]
=== Examples

[source,ruby]
----
# bad
include_examples 'examples'

# good
it_behaves_like 'examples'
----

[#references-rspecincludeexamples]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IncludeExamples

[#rspecindexedlet]
== RSpec/IndexedLet

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.20
| 2.23
|===

Do not set up test data using indexes (e.g., `item_1`, `item_2`).

It makes reading the test harder because it's not clear what exactly
is tested by this particular example.

The configurable options `AllowedIdentifiers` and `AllowedPatterns`
will also read those set in `Naming/VariableNumber`.

[#examples-rspecindexedlet]
=== Examples

[#_max_-1-_default__-rspecindexedlet]
==== `Max: 1 (default)`

[source,ruby]
----
# bad
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }

let(:item1) { create(:item) }
let(:item2) { create(:item) }

# good

let(:visible_item) { create(:item, visible: true) }
let(:invisible_item) { create(:item, visible: false) }
----

[#_max_-2_-rspecindexedlet]
==== `Max: 2`

[source,ruby]
----
# bad
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }
let(:item_3) { create(:item) }

# good
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }
----

[#_allowedidentifiers_-__item_1__-_item_2___-rspecindexedlet]
==== `AllowedIdentifiers: ['item_1', 'item_2']`

[source,ruby]
----
# good
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }
----

[#_allowedpatterns_-__item___-rspecindexedlet]
==== `AllowedPatterns: ['item']`

[source,ruby]
----
# good
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }
----

[#configurable-attributes-rspecindexedlet]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Max
| `1`
| Integer

| AllowedIdentifiers
| `[]`
| Array

| AllowedPatterns
| `[]`
| Array
|===

[#references-rspecindexedlet]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IndexedLet

[#rspecinstancespy]
== RSpec/InstanceSpy

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.12
| -
|===

Checks for `instance_double` used with `have_received`.

[#examples-rspecinstancespy]
=== Examples

[source,ruby]
----
# bad
it do
  foo = instance_double(Foo).as_null_object
  expect(foo).to have_received(:bar)
end

# good
it do
  foo = instance_spy(Foo)
  expect(foo).to have_received(:bar)
end
----

[#references-rspecinstancespy]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/InstanceSpy

[#rspecinstancevariable]
== RSpec/InstanceVariable

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.0
| 1.7
|===

Checks for instance variable usage in specs.

This cop can be configured with the option `AssignmentOnly` which
will configure the cop to only register offenses on instance
variable usage if the instance variable is also assigned within
the spec

[#examples-rspecinstancevariable]
=== Examples

[source,ruby]
----
# bad
describe MyClass do
  before { @foo = [] }
  it { expect(@foo).to be_empty }
end

# good
describe MyClass do
  let(:foo) { [] }
  it { expect(foo).to be_empty }
end
----

[#with-assignmentonly-configuration-rspecinstancevariable]
==== with AssignmentOnly configuration

[source,ruby]
----
# rubocop.yml
# RSpec/InstanceVariable:
#   AssignmentOnly: true

# bad
describe MyClass do
  before { @foo = [] }
  it { expect(@foo).to be_empty }
end

# allowed
describe MyClass do
  it { expect(@foo).to be_empty }
end

# good
describe MyClass do
  let(:foo) { [] }
  it { expect(foo).to be_empty }
end
----

[#configurable-attributes-rspecinstancevariable]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| AssignmentOnly
| `false`
| Boolean
|===

[#references-rspecinstancevariable]
=== References

* https://rspec.rubystyle.guide/#instance-variables
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/InstanceVariable

[#rspecisexpectedspecify]
== RSpec/IsExpectedSpecify

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.27
| -
|===

Check for `specify` with `is_expected` and one-liner expectations.

[#examples-rspecisexpectedspecify]
=== Examples

[source,ruby]
----
# bad
specify { is_expected.to be_truthy }

# good
it { is_expected.to be_truthy }

# good
specify do
  # ...
end
specify { expect(sqrt(4)).to eq(2) }
----

[#references-rspecisexpectedspecify]
=== References

* https://rspec.rubystyle.guide/#it-and-specify
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IsExpectedSpecify

[#rspecitbehaveslike]
== RSpec/ItBehavesLike

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.13
| -
|===

Checks that only one `it_behaves_like` style is used.

[#examples-rspecitbehaveslike]
=== Examples

[#_enforcedstyle_-it_behaves_like_-_default_-rspecitbehaveslike]
==== `EnforcedStyle: it_behaves_like` (default)

[source,ruby]
----
# bad
it_should_behave_like 'a foo'

# good
it_behaves_like 'a foo'
----

[#_enforcedstyle_-it_should_behave_like_-rspecitbehaveslike]
==== `EnforcedStyle: it_should_behave_like`

[source,ruby]
----
# bad
it_behaves_like 'a foo'

# good
it_should_behave_like 'a foo'
----

[#configurable-attributes-rspecitbehaveslike]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `it_behaves_like`
| `it_behaves_like`, `it_should_behave_like`
|===

[#references-rspecitbehaveslike]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ItBehavesLike

[#rspeciteratedexpectation]
== RSpec/IteratedExpectation

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.14
| -
|===

Check that `all` matcher is used instead of iterating over an array.

[#examples-rspeciteratedexpectation]
=== Examples

[source,ruby]
----
# bad
it 'validates users' do
  [user1, user2, user3].each { |user| expect(user).to be_valid }
end

# good
it 'validates users' do
  expect([user1, user2, user3]).to all(be_valid)
end
----

[#references-rspeciteratedexpectation]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IteratedExpectation

[#rspecleadingsubject]
== RSpec/LeadingSubject

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.7
| 1.14
|===

Enforce that subject is the first definition in the test.

[#examples-rspecleadingsubject]
=== Examples

[source,ruby]
----
# bad
let(:params) { blah }
subject { described_class.new(params) }

before { do_something }
subject { described_class.new(params) }

it { expect_something }
subject { described_class.new(params) }
it { expect_something_else }

# good
subject { described_class.new(params) }
let(:params) { blah }

# good
subject { described_class.new(params) }
before { do_something }

# good
subject { described_class.new(params) }
it { expect_something }
it { expect_something_else }
----

[#references-rspecleadingsubject]
=== References

* https://rspec.rubystyle.guide/#leading-subject
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/LeadingSubject

[#rspecleakyconstantdeclaration]
== RSpec/LeakyConstantDeclaration

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.35
| -
|===

Checks that no class, module, or constant is declared.

Constants, including classes and modules, when declared in a block
scope, are defined in global namespace, and leak between examples.

If several examples may define a `DummyClass`, instead of being a
blank slate class as it will be in the first example, subsequent
examples will be reopening it and modifying its behavior in
unpredictable ways.
Even worse when a class that exists in the codebase is reopened.

Anonymous classes are fine, since they don't result in global
namespace name clashes.

[#examples-rspecleakyconstantdeclaration]
=== Examples

[#constants-leak-between-examples-rspecleakyconstantdeclaration]
==== Constants leak between examples

[source,ruby]
----
# bad
describe SomeClass do
  OtherClass = Struct.new
  CONSTANT_HERE = 'I leak into global namespace'
end

# good
describe SomeClass do
  before do
    stub_const('OtherClass', Struct.new)
    stub_const('CONSTANT_HERE', 'I only exist during this example')
  end
end
----

[source,ruby]
----
# bad
describe SomeClass do
  class FooClass < described_class
    def double_that
      some_base_method * 2
    end
  end

  it { expect(FooClass.new.double_that).to eq(4) }
end

# good - anonymous class, no constant needs to be defined
describe SomeClass do
  let(:foo_class) do
    Class.new(described_class) do
      def double_that
        some_base_method * 2
      end
    end
  end

  it { expect(foo_class.new.double_that).to eq(4) }
end

# good - constant is stubbed
describe SomeClass do
  before do
    foo_class = Class.new(described_class) do
                  def do_something
                  end
                end
    stub_const('FooClass', foo_class)
  end

  it { expect(FooClass.new.double_that).to eq(4) }
end
----

[source,ruby]
----
# bad
describe SomeClass do
  module SomeModule
    class SomeClass
      def do_something
      end
    end
  end
end

# good
describe SomeClass do
  before do
    foo_class = Class.new(described_class) do
                  def do_something
                  end
                end
    stub_const('SomeModule::SomeClass', foo_class)
  end
end
----

[#references-rspecleakyconstantdeclaration]
=== References

* https://rspec.rubystyle.guide/#declare-constants
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/LeakyConstantDeclaration
* https://rspec.info/features/3-12/rspec-mocks/mutating-constants

[#rspecleakylocalvariable]
== RSpec/LeakyLocalVariable

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 3.8
| -
|===

Checks for local variables from outer scopes used inside examples.

Local variables assigned outside an example but used within it act
as shared state, which can make tests non-deterministic.

[#examples-rspecleakylocalvariable]
=== Examples

[source,ruby]
----
# bad - outside variable used in a hook
user = create(:user)

before { user.update(admin: true) }

# good
let(:user) { create(:user) }

before { user.update(admin: true) }

# bad - outside variable used in an example
user = create(:user)

it 'is persisted' do
  expect(user).to be_persisted
end

# good
let(:user) { create(:user) }

it 'is persisted' do
  expect(user).to be_persisted
end

# also good - assigning the variable within the example
it 'is persisted' do
  user = create(:user)

  expect(user).to be_persisted
end

# bad - outside variable passed to included examples
attrs = ['foo', 'bar']

it_behaves_like 'some examples', attrs

# good
it_behaves_like 'some examples' do
  let(:attrs) { ['foo', 'bar'] }
end

# good - when variable is used only as example description
attribute = 'foo'

it "#{attribute} is persisted" do
  expectations
end

# good - when variable is used only in example metadata
skip_message = 'not yet implemented'

it 'does something', skip: skip_message do
  expectations
end

# good - when variable is used only to include other examples
examples = foo ? 'some examples' : 'other examples'

it_behaves_like examples, another_argument
----

[#references-rspecleakylocalvariable]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/LeakyLocalVariable

[#rspecletbeforeexamples]
== RSpec/LetBeforeExamples

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.16
| 2.31
|===

Checks for `let` definitions that come after an example.

[#examples-rspecletbeforeexamples]
=== Examples

[source,ruby]
----
# bad
let(:foo) { bar }

it 'checks what foo does' do
  expect(foo).to be
end

let(:some) { other }

it 'checks what some does' do
  expect(some).to be
end

# good
let(:foo) { bar }
let(:some) { other }

it 'checks what foo does' do
  expect(foo).to be
end

it 'checks what some does' do
  expect(some).to be
end
----

[#references-rspecletbeforeexamples]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/LetBeforeExamples

[#rspecletsetup]
== RSpec/LetSetup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.7
| -
|===

Checks unreferenced `let!` calls being used for test setup.

[#examples-rspecletsetup]
=== Examples

[source,ruby]
----
# bad
let!(:my_widget) { create(:widget) }

it 'counts widgets' do
  expect(Widget.count).to eq(1)
end

# good
it 'counts widgets' do
  create(:widget)
  expect(Widget.count).to eq(1)
end

# good
before { create(:widget) }

it 'counts widgets' do
  expect(Widget.count).to eq(1)
end

# good
describe 'a widget' do
  let!(:my_widget) { create(:widget) }
  context 'when visiting its page' do
    let!(:my_widget) { create(:widget, name: 'Special') }
    it 'counts widgets' do
      expect(Widget.count).to eq(1)
    end
  end
end
----

[#references-rspecletsetup]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/LetSetup

[#rspecmatcharray]
== RSpec/MatchArray

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.19
| -
|===

Checks where `match_array` is used.

This cop checks for the following:

- Prefer `contain_exactly` when matching an array with values.
- Prefer `eq` when using `match_array` with an empty array literal.

[#examples-rspecmatcharray]
=== Examples

[source,ruby]
----
# bad
it { is_expected.to match_array([content1, content2]) }

# good
it { is_expected.to contain_exactly(content1, content2) }

# good
it { is_expected.to match_array([content] + array) }

# good
it { is_expected.to match_array(%w(tremble in fear foolish mortals)) }
----

[#references-rspecmatcharray]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MatchArray

[#rspecmessagechain]
== RSpec/MessageChain

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.7
| -
|===

Check that chains of messages are not being stubbed.

[#examples-rspecmessagechain]
=== Examples

[source,ruby]
----
# bad
allow(foo).to receive_message_chain(:bar, :baz).and_return(42)

# good
thing = Thing.new(baz: 42)
allow(foo).to receive(:bar).and_return(thing)
----

[#references-rspecmessagechain]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageChain

[#rspecmessageexpectation]
== RSpec/MessageExpectation

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| No
| 1.7
| 1.8
|===

Checks for consistent message expectation style.

This cop can be configured in your configuration using the
`EnforcedStyle` option and supports `--auto-gen-config`.

[#examples-rspecmessageexpectation]
=== Examples

[#_enforcedstyle_-allow_-_default_-rspecmessageexpectation]
==== `EnforcedStyle: allow` (default)

[source,ruby]
----
# bad
expect(foo).to receive(:bar)

# good
allow(foo).to receive(:bar)
----

[#_enforcedstyle_-expect_-rspecmessageexpectation]
==== `EnforcedStyle: expect`

[source,ruby]
----
# bad
allow(foo).to receive(:bar)

# good
expect(foo).to receive(:bar)
----

[#configurable-attributes-rspecmessageexpectation]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `allow`
| `allow`, `expect`
|===

[#references-rspecmessageexpectation]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageExpectation

[#rspecmessagespies]
== RSpec/MessageSpies

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.9
| -
|===

Checks that message expectations are set using spies.

This cop can be configured in your configuration using the
`EnforcedStyle` option and supports `--auto-gen-config`.

[#examples-rspecmessagespies]
=== Examples

[#_enforcedstyle_-have_received_-_default_-rspecmessagespies]
==== `EnforcedStyle: have_received` (default)

[source,ruby]
----
# bad
expect(foo).to receive(:bar)
do_something

# good
allow(foo).to receive(:bar) # or use instance_spy
do_something
expect(foo).to have_received(:bar)
----

[#_enforcedstyle_-receive_-rspecmessagespies]
==== `EnforcedStyle: receive`

[source,ruby]
----
# bad
allow(foo).to receive(:bar)
do_something
expect(foo).to have_received(:bar)

# good
expect(foo).to receive(:bar)
do_something
----

[#configurable-attributes-rspecmessagespies]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `have_received`
| `have_received`, `receive`
|===

[#references-rspecmessagespies]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageSpies

[#rspecmetadatastyle]
== RSpec/MetadataStyle

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.24
| -
|===

Use consistent metadata style.

This cop does not support autocorrection in the case of
`EnforcedStyle: hash` where the trailing metadata type is ambiguous.
(e.g. `describe 'Something', :a, b`)

[#examples-rspecmetadatastyle]
=== Examples

[#enforcedstyle_-symbol-_default_-rspecmetadatastyle]
==== EnforcedStyle: symbol (default)

[source,ruby]
----
# bad
describe 'Something', a: true

# good
describe 'Something', :a
----

[#enforcedstyle_-hash-rspecmetadatastyle]
==== EnforcedStyle: hash

[source,ruby]
----
# bad
describe 'Something', :a

# good
describe 'Something', a: true
----

[#configurable-attributes-rspecmetadatastyle]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `symbol`
| `hash`, `symbol`
|===

[#references-rspecmetadatastyle]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MetadataStyle

[#rspecmissingexamplegroupargument]
== RSpec/MissingExampleGroupArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.28
| -
|===

Checks that the first argument to an example group is not empty.

[#examples-rspecmissingexamplegroupargument]
=== Examples

[source,ruby]
----
# bad
describe do
end

RSpec.describe do
end

# good
describe TestedClass do
end

describe "A feature example" do
end
----

[#references-rspecmissingexamplegroupargument]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MissingExampleGroupArgument

[#rspecmissingexpectationtargetmethod]
== RSpec/MissingExpectationTargetMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 3.0
| -
|===

Checks if `.to`, `not_to` or `to_not` are used.

The RSpec::Expectations::ExpectationTarget must use `to`, `not_to` or
`to_not` to run. Therefore, this cop checks if other methods are used.

[#examples-rspecmissingexpectationtargetmethod]
=== Examples

[source,ruby]
----
# bad
expect(something).kind_of? Foo
is_expected == 42
expect{something}.eq? BarError

# good
expect(something).to be_a Foo
is_expected.to eq 42
expect{something}.to raise_error BarError
----

[#references-rspecmissingexpectationtargetmethod]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MissingExpectationTargetMethod

[#rspecmultipledescribes]
== RSpec/MultipleDescribes

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.0
| -
|===

Checks for multiple top-level example groups.

Multiple descriptions for the same class or module should either
be nested or separated into different test files.

[#examples-rspecmultipledescribes]
=== Examples

[source,ruby]
----
# bad
describe MyClass, '.do_something' do
end
describe MyClass, '.do_something_else' do
end

# good
describe MyClass do
  describe '.do_something' do
  end
  describe '.do_something_else' do
  end
end
----

[#references-rspecmultipledescribes]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleDescribes

[#rspecmultipleexpectations]
== RSpec/MultipleExpectations

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.7
| 1.21
|===

Checks if examples contain too many `expect` calls.

This cop is configurable using the `Max` option
and works with `--auto-gen-config`.

[#examples-rspecmultipleexpectations]
=== Examples

[source,ruby]
----
# bad
describe UserCreator do
  it 'builds a user' do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end

# good
describe UserCreator do
  it 'sets the users name' do
    expect(user.name).to eq("John")
  end

  it 'sets the users age' do
    expect(user.age).to eq(22)
  end
end
----

[#_aggregate_failures_-true_-_default_-rspecmultipleexpectations]
==== `aggregate_failures: true` (default)

[source,ruby]
----
# good - the cop ignores when RSpec aggregates failures
describe UserCreator do
  it 'builds a user', :aggregate_failures do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end
----

[#_aggregate_failures_-false_-rspecmultipleexpectations]
==== `aggregate_failures: false`

[source,ruby]
----
# Detected as an offense
describe UserCreator do
  it 'builds a user', aggregate_failures: false do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end
----

[#_max_-1_-_default_-rspecmultipleexpectations]
==== `Max: 1` (default)

[source,ruby]
----
# bad
describe UserCreator do
  it 'builds a user' do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end
----

[#_max_-2_-rspecmultipleexpectations]
==== `Max: 2`

[source,ruby]
----
# good
describe UserCreator do
  it 'builds a user' do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end
----

[#configurable-attributes-rspecmultipleexpectations]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Max
| `1`
| Integer
|===

[#references-rspecmultipleexpectations]
=== References

* https://rspec.rubystyle.guide/#expectation-per-example
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleExpectations
* http://betterspecs.org/#single

[#rspecmultiplememoizedhelpers]
== RSpec/MultipleMemoizedHelpers

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.43
| -
|===

Checks if example groups contain too many `let` and `subject` calls.

This cop is configurable using the `Max` option and the `AllowSubject`
which will configure the cop to only register offenses on calls to
`let` and not calls to `subject`.

[#examples-rspecmultiplememoizedhelpers]
=== Examples

[source,ruby]
----
# bad
describe MyClass do
  let(:foo) { [] }
  let(:bar) { [] }
  let!(:baz) { [] }
  let(:qux) { [] }
  let(:quux) { [] }
  let(:quuz) { {} }
end

describe MyClass do
  let(:foo) { [] }
  let(:bar) { [] }
  let!(:baz) { [] }

  context 'when stuff' do
    let(:qux) { [] }
    let(:quux) { [] }
    let(:quuz) { {} }
  end
end

# good
describe MyClass do
  let(:bar) { [] }
  let!(:baz) { [] }
  let(:qux) { [] }
  let(:quux) { [] }
  let(:quuz) { {} }
end

describe MyClass do
  context 'when stuff' do
    let(:foo) { [] }
    let(:bar) { [] }
    let!(:booger) { [] }
  end

  context 'when other stuff' do
    let(:qux) { [] }
    let(:quux) { [] }
    let(:quuz) { {} }
  end
end
----

[#when-disabling-allowsubject-configuration-rspecmultiplememoizedhelpers]
==== when disabling AllowSubject configuration

[source,ruby]
----
# rubocop.yml
# RSpec/MultipleMemoizedHelpers:
#   AllowSubject: false

# bad - `subject` counts towards memoized helpers
describe MyClass do
  subject { {} }
  let(:foo) { [] }
  let(:bar) { [] }
  let!(:baz) { [] }
  let(:qux) { [] }
  let(:quux) { [] }
end
----

[#with-max-configuration-rspecmultiplememoizedhelpers]
==== with Max configuration

[source,ruby]
----
# rubocop.yml
# RSpec/MultipleMemoizedHelpers:
#   Max: 1

# bad
describe MyClass do
  let(:foo) { [] }
  let(:bar) { [] }
end
----

[#configurable-attributes-rspecmultiplememoizedhelpers]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowSubject
| `true`
| Boolean

| Max
| `5`
| Integer
|===

[#references-rspecmultiplememoizedhelpers]
=== References

* https://rspec.rubystyle.guide/#let-blocks
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleMemoizedHelpers

[#rspecmultiplesubjects]
== RSpec/MultipleSubjects

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.16
| -
|===

Checks if an example group defines `subject` multiple times.

This cop does not support autocorrection in some cases.
The autocorrect behavior for this cop depends on the type of
duplication:

  - If multiple named subjects are defined then this probably indicates
    that the overwritten subjects (all subjects except the last
    definition) are effectively being used to define helpers. In this
    case they are replaced with `let`.

  - If multiple unnamed subjects are defined though then this can *only*
    be dead code and we remove the overwritten subject definitions.

  - If subjects are defined with `subject!` then we don't autocorrect.
    This is enough of an edge case that people can just move this to
    a `before` hook on their own

[#examples-rspecmultiplesubjects]
=== Examples

[source,ruby]
----
# bad
describe Foo do
  subject(:user) { User.new }
  subject(:post) { Post.new }
end

# good
describe Foo do
  let(:user) { User.new }
  subject(:post) { Post.new }
end

# bad (does not support autocorrection)
describe Foo do
  subject!(:user) { User.new }
  subject!(:post) { Post.new }
end

# good
describe Foo do
  before do
    User.new
    Post.new
  end
end
----

[#references-rspecmultiplesubjects]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleSubjects

[#rspecnamedsubject]
== RSpec/NamedSubject

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.5.3
| 2.15
|===

Checks for explicitly referenced test subjects.

RSpec lets you declare an "implicit subject" using `subject { ... }`
which allows for tests like `it { is_expected.to be_valid }`.
If you need to reference your test subject you should explicitly
name it using `subject(:your_subject_name) { ... }`. Your test subjects
should be the most important object in your tests so they deserve
a descriptive name.

This cop can be configured in your configuration using `EnforcedStyle`,
and `IgnoreSharedExamples` which will not report offenses for implicit
subjects in shared example groups.

[#examples-rspecnamedsubject]
=== Examples

[#_enforcedstyle_-always_-_default_-rspecnamedsubject]
==== `EnforcedStyle: always` (default)

[source,ruby]
----
# bad
RSpec.describe User do
  subject { described_class.new }

  it 'is valid' do
    expect(subject.valid?).to be(true)
  end
end

# good
RSpec.describe User do
  subject(:user) { described_class.new }

  it 'is valid' do
    expect(user.valid?).to be(true)
  end
end

# also good
RSpec.describe User do
  subject(:user) { described_class.new }

  it { is_expected.to be_valid }
end
----

[#_enforcedstyle_-named_only_-rspecnamedsubject]
==== `EnforcedStyle: named_only`

[source,ruby]
----
# bad
RSpec.describe User do
  subject(:user) { described_class.new }

  it 'is valid' do
    expect(subject.valid?).to be(true)
  end
end

# good
RSpec.describe User do
  subject(:user) { described_class.new }

  it 'is valid' do
    expect(user.valid?).to be(true)
  end
end

# also good
RSpec.describe User do
  subject { described_class.new }

  it { is_expected.to be_valid }
end

# acceptable
RSpec.describe User do
  subject { described_class.new }

  it 'is valid' do
    expect(subject.valid?).to be(true)
  end
end
----

[#configurable-attributes-rspecnamedsubject]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `always`
| `always`, `named_only`

| IgnoreSharedExamples
| `true`
| Boolean
|===

[#references-rspecnamedsubject]
=== References

* https://rspec.rubystyle.guide/#use-subject
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NamedSubject

[#rspecnestedgroups]
== RSpec/NestedGroups

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.7
| 2.13
|===

Checks for nested example groups.

This cop is configurable using the `Max` option
and supports `--auto-gen-config`.

[#examples-rspecnestedgroups]
=== Examples

[source,ruby]
----
# bad
context 'when using some feature' do
  let(:some)    { :various }
  let(:feature) { :setup   }

  context 'when user is signed in' do  # flagged by rubocop
    let(:user) do
      UserCreate.call(user_attributes)
    end

    let(:user_attributes) do
      {
        name: 'John',
        age:  22,
        role: role
      }
    end

    context 'when user is an admin' do # flagged by rubocop
      let(:role) { 'admin' }

      it 'blah blah'
      it 'yada yada'
    end
  end
end

# good
context 'using some feature as an admin' do
  let(:some)    { :various }
  let(:feature) { :setup   }

  let(:user) do
    UserCreate.call(
      name: 'John',
      age:  22,
      role: 'admin'
    )
  end

  it 'blah blah'
  it 'yada yada'
end
----

[#_max_-3_-_default_-rspecnestedgroups]
==== `Max: 3` (default)

[source,ruby]
----
# bad
describe Foo do
  context 'foo' do
    context 'bar' do
      context 'baz' do # flagged by rubocop
      end
    end
  end
end
----

[#_max_-2_-rspecnestedgroups]
==== `Max: 2`

[source,ruby]
----
# bad
describe Foo do
  context 'foo' do
    context 'bar' do # flagged by rubocop
      context 'baz' do # flagged by rubocop
      end
    end
  end
end
----

[#_allowedgroups_-__-_default__-rspecnestedgroups]
==== `AllowedGroups: [] (default)`

[source,ruby]
----
describe Foo do # <-- nested groups 1
  context 'foo' do # <-- nested groups 2
    context 'bar' do # <-- nested groups 3
    end
  end
end
----

[#_allowedgroups_-_path__-rspecnestedgroups]
==== `AllowedGroups: [path]`

[source,ruby]
----
describe Foo do # <-- nested groups 1
  path '/foo' do # <-- nested groups 1 (not counted)
    context 'bar' do # <-- nested groups 2
    end
  end
end
----

[#configurable-attributes-rspecnestedgroups]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Max
| `3`
| Integer

| AllowedGroups
| `[]`
| Array
|===

[#references-rspecnestedgroups]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NestedGroups

[#rspecnoexpectationexample]
== RSpec/NoExpectationExample

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| No
| No
| 2.13
| 2.14
|===

Checks if an example contains any expectation.

All RSpec's example and expectation methods are covered by default.
If you are using your own custom methods,
add the following configuration:

  RSpec:
    Language:
      Examples:
        Regular:
          - custom_it
      Expectations:
        - custom_expect

This cop can be customized with an allowed expectation methods pattern
with an `AllowedPatterns` option. ^expect_ and ^assert_ are allowed
by default.

[#examples-rspecnoexpectationexample]
=== Examples

[source,ruby]
----
# bad
it do
  a?
end

# good
it do
  expect(a?).to be(true)
end
----

[#_allowedpatterns_-configuration-rspecnoexpectationexample]
==== `AllowedPatterns` configuration

[source,ruby]
----
# .rubocop.yml
# RSpec/NoExpectationExample:
#   AllowedPatterns:
#     - ^expect_
#     - ^assert_
----

[source,ruby]
----
# bad
it do
  not_expect_something
end

# good
it do
  expect_something
end

it do
  assert_something
end
----

[#configurable-attributes-rspecnoexpectationexample]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowedPatterns
| `^expect_`, `^assert_`
| Array
|===

[#references-rspecnoexpectationexample]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoExpectationExample

[#rspecnottonot]
== RSpec/NotToNot

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.4
| -
|===

Checks for consistent method usage for negating expectations.

[#examples-rspecnottonot]
=== Examples

[#_enforcedstyle_-not_to_-_default_-rspecnottonot]
==== `EnforcedStyle: not_to` (default)

[source,ruby]
----
# bad
it '...' do
  expect(false).to_not be_true
end

# good
it '...' do
  expect(false).not_to be_true
end
----

[#_enforcedstyle_-to_not_-rspecnottonot]
==== `EnforcedStyle: to_not`

[source,ruby]
----
# bad
it '...' do
  expect(false).not_to be_true
end

# good
it '...' do
  expect(false).to_not be_true
end
----

[#configurable-attributes-rspecnottonot]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `not_to`
| `not_to`, `to_not`
|===

[#references-rspecnottonot]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NotToNot

[#rspecoutput]
== RSpec/Output

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Command-line only (Unsafe)
| 3.9
| -
|===

Checks for the use of output calls like puts and print in specs.

[#safety-rspecoutput]
=== Safety

This autocorrection is marked as unsafe because, in rare cases, print
statements can be used on purpose for integration testing and deleting
them will cause tests to fail.

[#examples-rspecoutput]
=== Examples

[source,ruby]
----
# bad
puts 'A debug message'
pp 'A debug message'
print 'A debug message'
----

[#references-rspecoutput]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Output

[#rspecoverwritingsetup]
== RSpec/OverwritingSetup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.14
| -
|===

Checks if there is a let/subject that overwrites an existing one.

[#examples-rspecoverwritingsetup]
=== Examples

[source,ruby]
----
# bad
let(:foo) { bar }
let(:foo) { baz }

subject(:foo) { bar }
let(:foo) { baz }

let(:foo) { bar }
let!(:foo) { baz }

# good
subject(:test) { something }
let(:foo) { bar }
let(:baz) { baz }
let!(:other) { other }
----

[#references-rspecoverwritingsetup]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/OverwritingSetup

[#rspecpending]
== RSpec/Pending

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| No
| 1.25
| -
|===

Checks for any pending or skipped examples.

[#examples-rspecpending]
=== Examples

[source,ruby]
----
# bad
describe MyClass do
  it "should be true"
end

describe MyClass do
  it "should be true", skip: true do
    expect(1).to eq(2)
  end
end

describe MyClass do
  it "should be true" do
    pending
  end
end

describe MyClass do
  xit "should be true" do
  end
end

# good
describe MyClass do
end
----

[#references-rspecpending]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Pending

[#rspecpendingwithoutreason]
== RSpec/PendingWithoutReason

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.16
| -
|===

Checks for pending or skipped examples without reason.

[#examples-rspecpendingwithoutreason]
=== Examples

[source,ruby]
----
# bad
pending 'does something' do
end

# bad
it 'does something', :pending do
end

# bad
it 'does something' do
  pending
end

# bad
xdescribe 'something' do
end

# bad
skip 'does something' do
end

# bad
it 'does something', :skip do
end

# bad
it 'does something' do
  skip
end

# bad
it 'does something'

# good
it 'does something' do
  pending 'reason'
end

# good
it 'does something' do
  skip 'reason'
end

# good
it 'does something', pending: 'reason' do
end

# good
it 'does something', skip: 'reason' do
end
----

[#references-rspecpendingwithoutreason]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/PendingWithoutReason

[#rspecpredicatematcher]
== RSpec/PredicateMatcher

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always (Unsafe)
| 1.16
| -
|===

Prefer using predicate matcher over using predicate method directly.

RSpec defines magic matchers for predicate methods.
This cop recommends to use the predicate matcher instead of using
predicate method directly.

[#examples-rspecpredicatematcher]
=== Examples

[#strict_-true_-enforcedstyle_-inflected-_default_-rspecpredicatematcher]
==== Strict: true, EnforcedStyle: inflected (default)

[source,ruby]
----
# bad
expect(foo.something?).to be_truthy

# good
expect(foo).to be_something

# also good - It checks "true" strictly.
expect(foo.something?).to be(true)
----

[#strict_-false_-enforcedstyle_-inflected-rspecpredicatematcher]
==== Strict: false, EnforcedStyle: inflected

[source,ruby]
----
# bad
expect(foo.something?).to be_truthy
expect(foo.something?).to be(true)

# good
expect(foo).to be_something
----

[#strict_-true_-enforcedstyle_-explicit-rspecpredicatematcher]
==== Strict: true, EnforcedStyle: explicit

[source,ruby]
----
# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be(true)

# bad - no autocorrect
expect(foo)
  .to be_something(<<~TEXT)
    bar
  TEXT

# good
expect(foo.something?(<<~TEXT)).to be(true)
  bar
TEXT
----

[#strict_-false_-enforcedstyle_-explicit-rspecpredicatematcher]
==== Strict: false, EnforcedStyle: explicit

[source,ruby]
----
# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be_truthy
----

[#configurable-attributes-rspecpredicatematcher]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Strict
| `true`
| Boolean

| EnforcedStyle
| `inflected`
| `inflected`, `explicit`

| AllowedExplicitMatchers
| `[]`
| Array
|===

[#references-rspecpredicatematcher]
=== References

* https://rspec.rubystyle.guide/#predicate-matchers
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/PredicateMatcher

[#rspecreceivecounts]
== RSpec/ReceiveCounts

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.26
| -
|===

Check for `once` and `twice` receive counts matchers usage.

[#examples-rspecreceivecounts]
=== Examples

[source,ruby]
----
# bad
expect(foo).to receive(:bar).exactly(1).times
expect(foo).to receive(:bar).exactly(2).times
expect(foo).to receive(:bar).at_least(1).times
expect(foo).to receive(:bar).at_least(2).times
expect(foo).to receive(:bar).at_most(1).times
expect(foo).to receive(:bar).at_most(2).times

# good
expect(foo).to receive(:bar).once
expect(foo).to receive(:bar).twice
expect(foo).to receive(:bar).at_least(:once)
expect(foo).to receive(:bar).at_least(:twice)
expect(foo).to receive(:bar).at_most(:once)
expect(foo).to receive(:bar).at_most(:twice).times
----

[#references-rspecreceivecounts]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReceiveCounts

[#rspecreceivemessages]
== RSpec/ReceiveMessages

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always (Unsafe)
| 2.23
| -
|===

Prefer `receive_messages` over multiple `receive`s on the same object.

[#safety-rspecreceivemessages]
=== Safety

The autocorrection is marked as unsafe, because it may change the
order of stubs. This in turn may cause e.g. variables to be called
before they are defined.

[#examples-rspecreceivemessages]
=== Examples

[source,ruby]
----
# bad
before do
  allow(Service).to receive(:foo).and_return(bar)
  allow(Service).to receive(:baz).and_return(qux)
end

# good
before do
  allow(Service).to receive_messages(foo: bar, baz: qux)
end

# good - ignore same message
before do
  allow(Service).to receive(:foo).and_return(bar)
  allow(Service).to receive(:foo).and_return(qux)
end
----

[#references-rspecreceivemessages]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReceiveMessages

[#rspecreceivenever]
== RSpec/ReceiveNever

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.28
| -
|===

Prefer `not_to receive(...)` over `receive(...).never`.

This cop only flags usage with `expect`. It ignores `allow` because
`allow(...).to receive(...).never` is a valid way to ensure a method
is not called, while `allow(...).not_to receive(...)` would have
different semantics.

[#examples-rspecreceivenever]
=== Examples

[source,ruby]
----
# bad
expect(foo).to receive(:bar).never

# good
expect(foo).not_to receive(:bar)

# not flagged by this cop
allow(foo).to receive(:bar).never
----

[#references-rspecreceivenever]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReceiveNever

[#rspecredundantaround]
== RSpec/RedundantAround

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.19
| -
|===

Remove redundant `around` hook.

[#examples-rspecredundantaround]
=== Examples

[source,ruby]
----
# bad
around do |example|
  example.run
end

# good
----

[#references-rspecredundantaround]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround

[#rspecredundantpredicatematcher]
== RSpec/RedundantPredicateMatcher

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.26
| -
|===

Checks for redundant predicate matcher.

[#examples-rspecredundantpredicatematcher]
=== Examples

[source,ruby]
----
# bad
expect(foo).to be_exist(bar)
expect(foo).not_to be_include(bar)
expect(foo).to be_all(bar)

# good
expect(foo).to exist(bar)
expect(foo).not_to include(bar)
expect(foo).to all be(bar)
----

[#references-rspecredundantpredicatematcher]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantPredicateMatcher

[#rspecremoveconst]
== RSpec/RemoveConst

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.26
| -
|===

Checks that `remove_const` is not used in specs.

[#examples-rspecremoveconst]
=== Examples

[source,ruby]
----
# bad
it 'does something' do
  Object.send(:remove_const, :SomeConstant)
end

before do
  SomeClass.send(:remove_const, :SomeConstant)
end
----

[#references-rspecremoveconst]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RemoveConst

[#rspecrepeateddescription]
== RSpec/RepeatedDescription

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.9
| -
|===

Check for repeated description strings in example groups.

[#examples-rspecrepeateddescription]
=== Examples

[source,ruby]
----
# bad
RSpec.describe User do
  it 'is valid' do
    # ...
  end

  it 'is valid' do
    # ...
  end
end

# good
RSpec.describe User do
  it 'is valid when first and last name are present' do
    # ...
  end

  it 'is valid when last name only is present' do
    # ...
  end
end

# good
RSpec.describe User do
  it 'is valid' do
    # ...
  end

  it 'is valid', :flag do
    # ...
  end
end
----

[#references-rspecrepeateddescription]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedDescription

[#rspecrepeatedexample]
== RSpec/RepeatedExample

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.10
| -
|===

Check for repeated examples within example groups.

[#examples-rspecrepeatedexample]
=== Examples

[source,ruby]
----
it 'is valid' do
  expect(user).to be_valid
end

it 'validates the user' do
  expect(user).to be_valid
end
----

[#references-rspecrepeatedexample]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedExample

[#rspecrepeatedexamplegroupbody]
== RSpec/RepeatedExampleGroupBody

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.38
| -
|===

Check for repeated describe and context block body.

[#examples-rspecrepeatedexamplegroupbody]
=== Examples

[source,ruby]
----
# bad
describe 'cool feature x' do
  it { cool_predicate }
end

describe 'cool feature y' do
  it { cool_predicate }
end

# good
describe 'cool feature' do
  it { cool_predicate }
end

describe 'another cool feature' do
  it { another_predicate }
end

# good
context 'when case x', :tag do
  it { cool_predicate }
end

context 'when case y' do
  it { cool_predicate }
end

# good
context Array do
  it { is_expected.to respond_to :each }
end

context Hash do
  it { is_expected.to respond_to :each }
end
----

[#references-rspecrepeatedexamplegroupbody]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedExampleGroupBody

[#rspecrepeatedexamplegroupdescription]
== RSpec/RepeatedExampleGroupDescription

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.38
| -
|===

Check for repeated example group descriptions.

[#examples-rspecrepeatedexamplegroupdescription]
=== Examples

[source,ruby]
----
# bad
describe 'cool feature' do
  # example group
end

describe 'cool feature' do
  # example group
end

# bad
context 'when case x' do
  # example group
end

describe 'when case x' do
  # example group
end

# good
describe 'cool feature' do
  # example group
end

describe 'another cool feature' do
  # example group
end

# good
context 'when case x' do
  # example group
end

context 'when another case' do
  # example group
end
----

[#references-rspecrepeatedexamplegroupdescription]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedExampleGroupDescription

[#rspecrepeatedincludeexample]
== RSpec/RepeatedIncludeExample

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.44
| -
|===

Check for repeated include of shared examples.

[#examples-rspecrepeatedincludeexample]
=== Examples

[source,ruby]
----
# bad
describe 'foo' do
  include_examples 'cool stuff'
  include_examples 'cool stuff'
end

# bad
describe 'foo' do
  it_behaves_like 'a cool', 'thing'
  it_behaves_like 'a cool', 'thing'
end

# bad
context 'foo' do
  it_should_behave_like 'a duck'
  it_should_behave_like 'a duck'
end

# good
describe 'foo' do
  include_examples 'cool stuff'
end

describe 'bar' do
  include_examples 'cool stuff'
end

# good
describe 'foo' do
  it_behaves_like 'a cool', 'thing'
  it_behaves_like 'a cool', 'person'
end

# good
context 'foo' do
  it_should_behave_like 'a duck'
  it_should_behave_like 'a goose'
end
----

[#references-rspecrepeatedincludeexample]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedIncludeExample

[#rspecrepeatedsubjectcall]
== RSpec/RepeatedSubjectCall

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.27
| -
|===

Checks for repeated calls to subject missing that it is memoized.

[#examples-rspecrepeatedsubjectcall]
=== Examples

[source,ruby]
----
# bad
it do
  subject
  expect { subject }.to not_change { A.count }
end

it do
  expect { subject }.to change { A.count }
  expect { subject }.to not_change { A.count }
end

# good
it do
  expect { my_method }.to change { A.count }
  expect { my_method }.to not_change { A.count }
end

# also good
it do
  expect { subject.a }.to change { A.count }
  expect { subject.b }.to not_change { A.count }
end
----

[#references-rspecrepeatedsubjectcall]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedSubjectCall

[#rspecreturnfromstub]
== RSpec/ReturnFromStub

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.16
| 1.22
|===

Checks for consistent style of stub's return setting.

Enforces either `and_return` or block-style return in the cases
where the returned value is constant. Ignores dynamic returned values
are the result would be different

This cop can be configured using the `EnforcedStyle` option

[#examples-rspecreturnfromstub]
=== Examples

[#_enforcedstyle_-and_return_-_default_-rspecreturnfromstub]
==== `EnforcedStyle: and_return` (default)

[source,ruby]
----
# bad
allow(Foo).to receive(:bar) { "baz" }
expect(Foo).to receive(:bar) { "baz" }

# good
allow(Foo).to receive(:bar).and_return("baz")
expect(Foo).to receive(:bar).and_return("baz")
# also good as the returned value is dynamic
allow(Foo).to receive(:bar) { bar.baz }
----

[#_enforcedstyle_-block_-rspecreturnfromstub]
==== `EnforcedStyle: block`

[source,ruby]
----
# bad
allow(Foo).to receive(:bar).and_return("baz")
expect(Foo).to receive(:bar).and_return("baz")

# good
allow(Foo).to receive(:bar) { "baz" }
expect(Foo).to receive(:bar) { "baz" }
# also good as the returned value is dynamic
allow(Foo).to receive(:bar).and_return(bar.baz)
----

[#configurable-attributes-rspecreturnfromstub]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `and_return`
| `and_return`, `block`
|===

[#references-rspecreturnfromstub]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReturnFromStub

[#rspecscatteredlet]
== RSpec/ScatteredLet

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.14
| 2.31
|===

Checks for let scattered across the example group.

Group lets together

[#examples-rspecscatteredlet]
=== Examples

[source,ruby]
----
# bad
describe Foo do
  let(:foo) { 1 }
  subject { Foo }
  let(:bar) { 2 }
  before { prepare }
  let!(:baz) { 3 }
end

# good
describe Foo do
  subject { Foo }
  before { prepare }
  let(:foo) { 1 }
  let(:bar) { 2 }
  let!(:baz) { 3 }
end
----

[#references-rspecscatteredlet]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ScatteredLet

[#rspecscatteredsetup]
== RSpec/ScatteredSetup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Command-line only
| 1.10
| 2.31
|===

Checks for setup scattered across multiple hooks in an example group.

Unify `before` and `after` hooks when possible.
However, `around` hooks are allowed to be defined multiple times,
as unifying them would typically make the code harder to read.
Hooks defined in class methods are also ignored.

[#examples-rspecscatteredsetup]
=== Examples

[source,ruby]
----
# bad
describe Foo do
  before { setup1 }
  before { setup2 }
end

# good
describe Foo do
  before do
    setup1
    setup2
  end
end

# good
describe Foo do
  around { |example| before1; example.call; after1 }
  around { |example| before2; example.call; after2 }
end

# good
describe Foo do
  before { setup1 }
  def self.setup
    before { setup2 }
  end
end
----

[#references-rspecscatteredsetup]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ScatteredSetup

[#rspecsharedcontext]
== RSpec/SharedContext

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.13
| -
|===

Checks for proper shared_context and shared_examples usage.

If there are no examples defined, use shared_context.
If there is no setup defined, use shared_examples.

[#examples-rspecsharedcontext]
=== Examples

[source,ruby]
----
# bad
RSpec.shared_context 'only examples here' do
  it 'does x' do
  end

  it 'does y' do
  end
end

# good
RSpec.shared_examples 'only examples here' do
  it 'does x' do
  end

  it 'does y' do
  end
end
----

[source,ruby]
----
# bad
RSpec.shared_examples 'only setup here' do
  subject(:foo) { :bar }

  let(:baz) { :bazz }

  before do
    something
  end
end

# good
RSpec.shared_context 'only setup here' do
  subject(:foo) { :bar }

  let(:baz) { :bazz }

  before do
    something
  end
end
----

[#references-rspecsharedcontext]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SharedContext

[#rspecsharedexamples]
== RSpec/SharedExamples

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.25
| 2.26
|===

Checks for consistent style for shared example names.

Enforces either `string` or `symbol` for shared example names.

This cop can be configured using the `EnforcedStyle` option

[#examples-rspecsharedexamples]
=== Examples

[#_enforcedstyle_-string_-_default_-rspecsharedexamples]
==== `EnforcedStyle: string` (default)

[source,ruby]
----
# bad
it_behaves_like :foo_bar_baz
it_should_behave_like :foo_bar_baz
shared_examples :foo_bar_baz
shared_examples_for :foo_bar_baz
include_examples :foo_bar_baz

# good
it_behaves_like 'foo bar baz'
it_should_behave_like 'foo bar baz'
shared_examples 'foo bar baz'
shared_examples_for 'foo bar baz'
include_examples 'foo bar baz'
----

[#_enforcedstyle_-symbol_-rspecsharedexamples]
==== `EnforcedStyle: symbol`

[source,ruby]
----
# bad
it_behaves_like 'foo bar baz'
it_should_behave_like 'foo bar baz'
shared_examples 'foo bar baz'
shared_examples_for 'foo bar baz'
include_examples 'foo bar baz'

# good
it_behaves_like :foo_bar_baz
it_should_behave_like :foo_bar_baz
shared_examples :foo_bar_baz
shared_examples_for :foo_bar_baz
include_examples :foo_bar_baz
----

[#configurable-attributes-rspecsharedexamples]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `string`
| `string`, `symbol`
|===

[#references-rspecsharedexamples]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SharedExamples

[#rspecsingleargumentmessagechain]
== RSpec/SingleArgumentMessageChain

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.9
| 1.10
|===

Checks that chains of messages contain more than one element.

[#examples-rspecsingleargumentmessagechain]
=== Examples

[source,ruby]
----
# bad
allow(foo).to receive_message_chain(:bar).and_return(42)

# good
allow(foo).to receive(:bar).and_return(42)

# also good
allow(foo).to receive(:bar, :baz)
allow(foo).to receive("bar.baz")
----

[#references-rspecsingleargumentmessagechain]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SingleArgumentMessageChain

[#rspecskipblockinsideexample]
== RSpec/SkipBlockInsideExample

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.19
| -
|===

Checks for passing a block to `skip` within examples.

[#examples-rspecskipblockinsideexample]
=== Examples

[source,ruby]
----
# bad
it 'does something' do
  skip 'not yet implemented' do
    do_something
  end
end

# good
it 'does something' do
  skip 'not yet implemented'
  do_something
end

# good - when outside example
skip 'not yet implemented' do
end
----

[#references-rspecskipblockinsideexample]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SkipBlockInsideExample

[#rspecsortmetadata]
== RSpec/SortMetadata

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 2.14
| -
|===

Sort RSpec metadata alphabetically.

Only the trailing metadata is sorted.

[#examples-rspecsortmetadata]
=== Examples

[source,ruby]
----
# bad
describe 'Something', :b, :a
context 'Something', foo: 'bar', baz: true
it 'works', :b, :a, foo: 'bar', baz: true

# good
describe 'Something', :a, :b
context 'Something', baz: true, foo: 'bar'
it 'works', :a, :b, baz: true, foo: 'bar'

# good, trailing metadata is sorted
describe 'Something', 'description', :a, :b, :z
context 'Something', :z, variable, :a, :b
----

[#references-rspecsortmetadata]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SortMetadata

[#rspecspecfilepathformat]
== RSpec/SpecFilePathFormat

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.24
| 3.8
|===

Checks that spec file paths are consistent and well-formed.

[#examples-rspecspecfilepathformat]
=== Examples

[source,ruby]
----
# bad
whatever_spec.rb         # describe MyClass
my_class_spec.rb         # describe MyClass, '#method'

# good
my_class_spec.rb         # describe MyClass
my_class_method_spec.rb  # describe MyClass, '#method'
my_class/method_spec.rb  # describe MyClass, '#method'
----

[#_customtransform_-_rubocop__rubocop_-rspec__rspec__-_default_-rspecspecfilepathformat]
==== `CustomTransform: {RuboCop=>rubocop, RSpec=>rspec}` (default)

[source,ruby]
----
# good
rubocop_spec.rb          # describe RuboCop
rspec_spec.rb            # describe RSpec
----

[#_ignoremethods_-false_-_default_-rspecspecfilepathformat]
==== `IgnoreMethods: false` (default)

[source,ruby]
----
# bad
my_class_spec.rb         # describe MyClass, '#method'
----

[#_ignoremethods_-true_-rspecspecfilepathformat]
==== `IgnoreMethods: true`

[source,ruby]
----
# good
my_class_spec.rb         # describe MyClass, '#method'
----

[#_ignoremetadata_-_type__routing__-_default_-rspecspecfilepathformat]
==== `IgnoreMetadata: {type=>routing}` (default)

[source,ruby]
----
# good
whatever_spec.rb         # describe MyClass, type: :routing do; end
----

[#_enforcedinflector_-active_support_-rspecspecfilepathformat]
==== `EnforcedInflector: active_support`

[source,ruby]
----
# Enable to use ActiveSupport's inflector for custom acronyms
# like HTTP, etc. Set to "default" by default.
# Configure `InflectorPath` with the path to the inflector file.
# The default is ./config/initializers/inflections.rb.
----

[#configurable-attributes-rspecspecfilepathformat]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Include
| `+**/*_spec.rb+`
| Array

| Exclude
| `+**/spec/routing/**/*+`
| Array

| CustomTransform
| `{"RuboCop" => "rubocop", "RSpec" => "rspec"}`
| 

| IgnoreMethods
| `false`
| Boolean

| IgnoreMetadata
| `{"type" => "routing"}`
| 

| InflectorPath
| `./config/initializers/inflections.rb`
| String

| EnforcedInflector
| `default`
| `<none>`
|===

[#references-rspecspecfilepathformat]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathFormat

[#rspecspecfilepathsuffix]
== RSpec/SpecFilePathSuffix

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.24
| -
|===

Checks that spec file paths suffix are consistent and well-formed.

[#examples-rspecspecfilepathsuffix]
=== Examples

[source,ruby]
----
# bad
my_class/foo_specorb.rb   # describe MyClass
spec/models/user.rb       # describe User
spec/models/user_specxrb  # describe User

# good
my_class_spec.rb          # describe MyClass

# good - shared examples are allowed
spec/models/user.rb       # shared_examples_for 'foo'
----

[#configurable-attributes-rspecspecfilepathsuffix]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Include
| `+**/*_spec*rb*+`, `+**/spec/**/*+`
| Array
|===

[#references-rspecspecfilepathsuffix]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix

[#rspecstubbedmock]
== RSpec/StubbedMock

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.44
| -
|===

Checks that message expectations do not have a configured response.

[#examples-rspecstubbedmock]
=== Examples

[source,ruby]
----
# bad
expect(foo).to receive(:bar).with(42).and_return("hello world")

# good (without spies)
allow(foo).to receive(:bar).with(42).and_return("hello world")
expect(foo).to receive(:bar).with(42)
----

[#references-rspecstubbedmock]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StubbedMock

[#rspecsubjectdeclaration]
== RSpec/SubjectDeclaration

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.5
| -
|===

Ensure that subject is defined using subject helper.

[#examples-rspecsubjectdeclaration]
=== Examples

[source,ruby]
----
# bad
let(:subject) { foo }
let!(:subject) { foo }
subject(:subject) { foo }
subject!(:subject) { foo }

# bad
block = -> {}
let(:subject, &block)

# good
subject(:test_subject) { foo }
----

[#references-rspecsubjectdeclaration]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SubjectDeclaration

[#rspecsubjectstub]
== RSpec/SubjectStub

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.7
| 2.8
|===

Checks for stubbed test subjects.

Checks nested subject stubs for innermost subject definition
when subject is also defined in parent example groups.

[#examples-rspecsubjectstub]
=== Examples

[source,ruby]
----
# bad
describe Article do
  subject(:article) { Article.new }

  it 'indicates that the author is unknown' do
    allow(article).to receive(:author).and_return(nil)
    expect(article.description).to include('by an unknown author')
  end
end

# bad
describe Article do
  subject(:foo) { Article.new }

  context 'nested subject' do
    subject(:article) { Article.new }

    it 'indicates that the author is unknown' do
      allow(article).to receive(:author).and_return(nil)
      expect(article.description).to include('by an unknown author')
    end
  end
end

# good
describe Article do
  subject(:article) { Article.new(author: nil) }

  it 'indicates that the author is unknown' do
    expect(article.description).to include('by an unknown author')
  end
end
----

[#references-rspecsubjectstub]
=== References

* https://rspec.rubystyle.guide/#dont-stub-subject
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SubjectStub
* https://robots.thoughtbot.com/don-t-stub-the-system-under-test
* https://penelope.zone/2015/12/27/introducing-rspec-smells-and-where-to-find-them.html#smell-1-stubjec

[#rspecundescriptiveliteralsdescription]
== RSpec/UndescriptiveLiteralsDescription

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 2.29
| -
|===

Description should be descriptive.

If example group or example contains only `execute string`, numbers
and regular expressions, the description is not clear.

[#examples-rspecundescriptiveliteralsdescription]
=== Examples

[source,ruby]
----
# bad
describe `time` do
 # ...
end

# bad
context /when foo/ do
  # ...
end

# bad
it 10000 do
  # ...
end

# good
describe Foo do
  # ...
end

# good
describe '#foo' do
  # ...
end

# good
context "when #{foo} is bar" do
  # ...
end

# good
it 'does something' do
  # ...
end
----

[#references-rspecundescriptiveliteralsdescription]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/UndescriptiveLiteralsDescription

[#rspecunspecifiedexception]
== RSpec/UnspecifiedException

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.30
| -
|===

Checks for a specified error in checking raised errors.

Enforces one of an Exception type, a string, or a regular
expression to match against the exception message as a parameter
to `raise_error`

[#examples-rspecunspecifiedexception]
=== Examples

[source,ruby]
----
# bad
expect {
  raise StandardError.new('error')
}.to raise_error

# good
expect {
  raise StandardError.new('error')
}.to raise_error(StandardError)

expect {
  raise StandardError.new('error')
}.to raise_error('error')

expect {
  raise StandardError.new('error')
}.to raise_error(/err/)

expect { do_something }.not_to raise_error
----

[#references-rspecunspecifiedexception]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/UnspecifiedException

[#rspecvariabledefinition]
== RSpec/VariableDefinition

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.40
| -
|===

Checks that memoized helpers names are symbols or strings.

[#examples-rspecvariabledefinition]
=== Examples

[#enforcedstyle_-symbols-_default_-rspecvariabledefinition]
==== EnforcedStyle: symbols (default)

[source,ruby]
----
# bad
subject('user') { create_user }
let('user_name') { 'Adam' }

# good
subject(:user) { create_user }
let(:user_name) { 'Adam' }
----

[#enforcedstyle_-strings-rspecvariabledefinition]
==== EnforcedStyle: strings

[source,ruby]
----
# bad
subject(:user) { create_user }
let(:user_name) { 'Adam' }

# good
subject('user') { create_user }
let('user_name') { 'Adam' }
----

[#configurable-attributes-rspecvariabledefinition]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `symbols`
| `symbols`, `strings`
|===

[#references-rspecvariabledefinition]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VariableDefinition

[#rspecvariablename]
== RSpec/VariableName

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.40
| 2.13
|===

Checks that memoized helper names use the configured style.

Variables can be excluded from checking using the `AllowedPatterns`
option.

[#examples-rspecvariablename]
=== Examples

[#enforcedstyle_-snake_case-_default_-rspecvariablename]
==== EnforcedStyle: snake_case (default)

[source,ruby]
----
# bad
subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }

# good
subject(:user_name_1) { 'Adam' }
let(:user_name_2) { 'Adam' }
----

[#enforcedstyle_-camelcase-rspecvariablename]
==== EnforcedStyle: camelCase

[source,ruby]
----
# bad
subject(:user_name_1) { 'Adam' }
let(:user_name_2) { 'Adam' }

# good
subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }
----

[#allowedpatterns-configuration-rspecvariablename]
==== AllowedPatterns configuration

[source,ruby]
----
# rubocop.yml
# RSpec/VariableName:
#   EnforcedStyle: snake_case
#   AllowedPatterns:
#     - ^userFood
----

[source,ruby]
----
# okay because it matches the `^userFood` regex in `AllowedPatterns`
subject(:userFood_1) { 'spaghetti' }
let(:userFood_2) { 'fettuccine' }
----

[#configurable-attributes-rspecvariablename]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `snake_case`
| `snake_case`, `camelCase`

| AllowedPatterns
| `[]`
| Array
|===

[#references-rspecvariablename]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VariableName

[#rspecverifieddoublereference]
== RSpec/VerifiedDoubleReference

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always (Unsafe)
| 2.10.0
| 3.4
|===

Checks for consistent verified double reference style.

[#safety-rspecverifieddoublereference]
=== Safety

This cop is unsafe because the correction requires loading the class.
Loading before stubbing causes RSpec to only allow instance methods
to be stubbed.

[#examples-rspecverifieddoublereference]
=== Examples

[source,ruby]
----
# bad
let(:foo) do
  instance_double('ClassName', method_name: 'returned_value')
end

# good
let(:foo) do
  instance_double(ClassName, method_name: 'returned_value')
end
----

[#reference-is-any-dynamic-variable_-no-enforcement-rspecverifieddoublereference]
==== Reference is any dynamic variable. No enforcement

[source,ruby]
----
# good
let(:foo) do
  instance_double(@klass, method_name: 'returned_value')
end
----

[#references-rspecverifieddoublereference]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference
* https://rspec.info/features/3-12/rspec-mocks/verifying-doubles

[#rspecverifieddoubles]
== RSpec/VerifiedDoubles

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.2.1
| 1.5
|===

Prefer using verifying doubles over normal doubles.

[#examples-rspecverifieddoubles]
=== Examples

[source,ruby]
----
# bad
let(:foo) do
  double("ClassName", method_name: 'returned value')
end

# bad
let(:foo) do
  spy("ClassName", method_name: 'returned value')
end

# good
let(:foo) do
  instance_double("ClassName", method_name: 'returned value')
end

# good
let(:foo) do
  class_double("ClassName", method_name: 'returned value')
end

# good
let(:foo) do
  object_double("some object", method_name: 'returned value')
end
----

[#_ignorenameless_-true-_default__-rspecverifieddoubles]
==== `IgnoreNameless: true (default)`

[source,ruby]
----
# good
let(:foo) do
  double(method_name: 'returned value')
end

# good
let(:foo) do
  double
end
----

[#_ignorenameless_-false_-rspecverifieddoubles]
==== `IgnoreNameless: false`

[source,ruby]
----
# bad
let(:foo) do
  double(method_name: 'returned value')
end

# bad
let(:foo) do
  double
end
----

[#_ignoresymbolicnames_-false-_default__-rspecverifieddoubles]
==== `IgnoreSymbolicNames: false (default)`

[source,ruby]
----
# bad
let(:foo) do
  double(:foo)
end
----

[#_ignoresymbolicnames_-true_-rspecverifieddoubles]
==== `IgnoreSymbolicNames: true`

[source,ruby]
----
# good
let(:foo) do
  double(:foo)
end
----

[#configurable-attributes-rspecverifieddoubles]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| IgnoreNameless
| `true`
| Boolean

| IgnoreSymbolicNames
| `false`
| Boolean
|===

[#references-rspecverifieddoubles]
=== References

* https://rspec.rubystyle.guide/#doubles
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubles
* https://rspec.info/features/3-12/rspec-mocks/verifying-doubles

[#rspecvoidexpect]
== RSpec/VoidExpect

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| No
| 1.16
| -
|===

Checks void `expect()`.

[#examples-rspecvoidexpect]
=== Examples

[source,ruby]
----
# bad
expect(something)

# good
expect(something).to be(1)
----

[#references-rspecvoidexpect]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VoidExpect

[#rspecyield]
== RSpec/Yield

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 1.32
| -
|===

Checks for calling a block within a stub.

[#examples-rspecyield]
=== Examples

[source,ruby]
----
# bad
allow(foo).to receive(:bar) { |&block| block.call(1) }

# good
expect(foo).to receive(:bar).and_yield(1)
----

[#references-rspecyield]
=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Yield
