RSpec
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.
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.
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
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.
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.
RSpec/BeEmpty
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.
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.
Examples
# 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)
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
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
# 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
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
NegatedMatcher: ~ (default)
# 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
# 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 }
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
.
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.
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.
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.
This cop can be customized allowed context description pattern
with AllowedPatterns
. By default, there are no checking by pattern.
Examples
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
IgnoredMetadata
configuration
# .rubocop.yml
# RSpec/DescribeClass:
# IgnoredMetadata:
# type:
# - request
# - controller
# 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
Name | Default value | Configurable values |
---|---|---|
Exclude |
|
Array |
IgnoredMetadata |
|
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.
RSpec/DescribeSymbol
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
1.15 |
- |
Avoid describing symbols.
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.
Examples
EnforcedStyle: described_class
(default)
# bad
describe MyClass do
subject { MyClass.do_something }
end
# good
describe MyClass do
subject { described_class.do_something }
end
RSpec/DescribedClassModuleWrapping
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
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:
RSpec/DuplicatedMetadata
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
2.16 |
- |
Avoid duplicated metadata.
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
usage
# 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
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.
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
# 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
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.
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.
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
# 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 }
RSpec/EmptyLineAfterSubject
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.
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.
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.
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.
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
# always good
specify do
result = service.call
expect(result).to be(true)
end
EnforcedStyle: always_allow
(default)
# 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
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.
The autocorrect is experimental - use with care! It can be configured with CustomTransform (e.g. have ⇒ has) and IgnoredWords (e.g. only).
Use the DisallowedExamples setting to prevent unclear or insufficient descriptions. Please note that this config will not be treated as case sensitive.
Examples
# bad
it 'should find nothing' do
end
it 'will find nothing' do
end
# good
it 'finds nothing' do
end
# bad
it 'it does things' do
end
# good
it 'does things' do
end
RSpec/ExcessiveDocstringSpacing
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
2.5 |
- |
Checks for excessive whitespace in example descriptions.
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.
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 object and attribute as arguments to the matcher or passing a block that reads the attribute value.
This cop can be configured using the EnforcedStyle
option.
Examples
EnforcedStyle: method_call
(default)
# 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 }
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
.
RSpec/ExpectInLet
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
.
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
# 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
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
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
EnforcedStyle: implicit
(default)
# bad
before(:each) do
# ...
end
# bad
before(:example) do
# ...
end
# good
before do
# ...
end
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.
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.
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.
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
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
EnforcedStyle: single_line_only
(default)
# 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
# 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
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
Max: 1 (default)
# 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
# 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) }
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
.
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
# 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
# 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
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.
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
RSpec/IteratedExpectation
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
1.14 |
- |
Check that all
matcher is used instead of iterating over an array.
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
# 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 }
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
Constants leak between examples
# 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
# 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
# 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
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
# 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
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.
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.
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.
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
.
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
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
)
RSpec/MissingExampleGroupArgument
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.
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.
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
# 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)
# 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
# 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
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
# 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
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 abefore
hook on their own
Examples
# 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
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
EnforcedStyle: always
(default)
# 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
# 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
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
# 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)
# bad
describe Foo do
context 'foo' do
context 'bar' do
context 'baz' do # flagged by rubocop
end
end
end
end
Max: 2
# bad
describe Foo do
context 'foo' do
context 'bar' do # flagged by rubocop
context 'baz' do # flagged by rubocop
end
end
end
end
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.
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.
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.
RSpec/Pending
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Disabled |
Yes |
No |
1.25 |
- |
Checks for any pending or skipped examples.
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
# 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
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
Strict: true, EnforcedStyle: inflected (default)
# 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
# bad
expect(foo.something?).to be_truthy
expect(foo.something?).to be(true)
# good
expect(foo).to be_something
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
# 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
RSpec/ReceiveMessages
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always (Unsafe) |
2.23 |
- |
Checks for multiple messages stubbed on the same object.
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
# 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
RSpec/ReceiveNever
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
1.28 |
- |
Prefer not_to receive(…)
over receive(…).never
.
RSpec/RedundantAround
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
2.19 |
- |
Remove redundant around
hook.
RSpec/RedundantPredicateMatcher
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
2.26 |
- |
Checks for redundant predicate matcher.
RSpec/RemoveConst
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
# 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
RSpec/RepeatedExample
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
# 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
RSpec/RepeatedExampleGroupDescription
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
1.38 |
- |
Check for repeated example group descriptions.
Examples
# 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
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
# 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
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
# 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
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
EnforcedStyle: and_return
(default)
# 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 }
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
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
, after
, and around
hooks when possible.
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
# 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
# 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
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
EnforcedStyle: string
(default)
# 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
# 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
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.
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.
RSpec/SortMetadata
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
2.14 |
- |
Sort RSpec metadata alphabetically.
RSpec/SpecFilePathFormat
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
2.24 |
- |
Checks that spec file paths are consistent and well-formed.
Examples
# 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'
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
# 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'
RSpec/StringAsInstanceDoubleConstant
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
No |
Always (Unsafe) |
3.1 |
- |
Do not use a string as instance_double
constant.
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.
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.
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
# 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
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.
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
# 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
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
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
EnforcedStyle: snake_case (default)
# bad
subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }
# good
subject(:user_name_1) { 'Adam' }
let(:user_name_2) { 'Adam' }
RSpec/VerifiedDoubleReference
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always (Unsafe) |
2.10.0 |
2.12 |
Checks for consistent verified double reference style.
Only investigates references that are one of the supported styles.
This cop can be configured in your configuration using the
EnforcedStyle
option and supports --auto-gen-config
.
Examples
EnforcedStyle: constant
(default)
# bad
let(:foo) do
instance_double('ClassName', method_name: 'returned_value')
end
# good
let(:foo) do
instance_double(ClassName, method_name: 'returned_value')
end
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
# bad
let(:foo) do
double(method_name: 'returned value')
end
# bad
let(:foo) do
double("ClassName", method_name: 'returned value')
end
# good
let(:foo) do
instance_double("ClassName", method_name: 'returned value')
end
RSpec/VoidExpect
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
No |
1.16 |
- |
Checks void expect()
.