Skip to content

Commit

Permalink
InstanceVariable: Also recommend local variable
Browse files Browse the repository at this point in the history
Often, a local variable is a better choice than a `let`. For example,
an object that will only be used in a single example.

When we use too many `let`s, examples become hard to understand, because
it is not clear which are applicable to a given example.

So, we should recommend both local variables and `let`s.
  • Loading branch information
jaredbeck committed Aug 20, 2018
1 parent 75d5fe7 commit 79a036d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* `RSpec/InstanceVariable` - Recommend local variables in addition to `let`. ([@jaredbeck][])

## 1.28.0 (2018-08-14)

* Add `RSpec/ReceiveNever` cop enforcing usage of `not_to receive` instead of `never` matcher. ([@Darhazer][])
Expand Down Expand Up @@ -328,6 +330,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@bquorning]: https://github.com/bquorning
[@deivid-rodriguez]: https://github.com/deivid-rodriguez
[@geniou]: https://github.com/geniou
[@jaredbeck]: https://github.com/jaredbeck
[@jawshooah]: https://github.com/jawshooah
[@nevir]: https://github.com/nevir
[@nijikon]: https://github.com/nijikon
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/instance_variable.rb
Expand Up @@ -47,7 +47,7 @@ module RSpec
# end
#
class InstanceVariable < Cop
MSG = 'Use `let` instead of an instance variable.'.freeze
MSG = 'Replace instance variable with local variable or `let`.'.freeze

EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL

Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/rspec/instance_variable_spec.rb
Expand Up @@ -6,7 +6,7 @@
describe MyClass do
before { @foo = [] }
it { expect(@foo).to be_empty }
^^^^ Use `let` instead of an instance variable.
^^^^ Replace instance variable with local variable or `let`.
end
RUBY
end
Expand All @@ -24,7 +24,7 @@
expect_offense(<<-RUBY)
shared_examples 'shared example' do
it { expect(@foo).to be_empty }
^^^^ Use `let` instead of an instance variable.
^^^^ Replace instance variable with local variable or `let`.
end
RUBY
end
Expand Down Expand Up @@ -77,7 +77,7 @@ def serialize
describe MyClass do
before { @foo = [] }
it { expect(@foo).to be_empty }
^^^^ Use `let` instead of an instance variable.
^^^^ Replace instance variable with local variable or `let`.
end
RUBY
end
Expand Down

0 comments on commit 79a036d

Please sign in to comment.