forked from rubocop/rubocop-rspec
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/ExpectInHook
cop (rubocop#445)
- Loading branch information
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Do not use `expect` in hooks such as `before`. | ||
# | ||
# @example | ||
# # 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 | ||
class ExpectInHook < Cop | ||
MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'.freeze | ||
HOOKS = Hooks::ALL.node_pattern_union.freeze | ||
|
||
def_node_matcher :hook, <<-PATTERN | ||
(block (send _ $#{HOOKS} ...) _ $!nil) | ||
PATTERN | ||
|
||
def_node_search :expect, <<-PATTERN | ||
{ | ||
#{Expectations::ALL.send_pattern} | ||
#{Expectations::ALL.block_pattern} | ||
} | ||
PATTERN | ||
|
||
def on_block(node) | ||
hook(node) do |hook_name, body| | ||
expect(body) do |expect| | ||
method = send_node(expect) | ||
add_offense(method, :selector, | ||
message(method, hook_name)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(expect, hook) | ||
format(MSG, expect: expect.method_name, hook: hook) | ||
end | ||
|
||
def send_node(node) | ||
return node if node.send_type? | ||
node.children.first | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::ExpectInHook do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'adds an offense for `expect` in `before` hook' do | ||
expect_offense(<<-RUBY) | ||
before do | ||
expect(something).to eq('foo') | ||
^^^^^^ Do not use `expect` in `before` hook | ||
expect_any_instance_of(Something).to receive(:foo) | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `before` hook | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds an offense for `expect` in `after` hook' do | ||
expect_offense(<<-RUBY) | ||
after do | ||
expect(something).to eq('foo') | ||
^^^^^^ Do not use `expect` in `after` hook | ||
expect_any_instance_of(Something).to receive(:foo) | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `after` hook | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds an offense for `expect` in `around` hook' do | ||
expect_offense(<<-RUBY) | ||
around do | ||
expect(something).to eq('foo') | ||
^^^^^^ Do not use `expect` in `around` hook | ||
expect_any_instance_of(Something).to receive(:foo) | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `around` hook | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds an offense for `expect` with block in `before` hook' do | ||
expect_offense(<<-RUBY) | ||
before do | ||
expect { something }.to eq('foo') | ||
^^^^^^ Do not use `expect` in `before` hook | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts an empty `before` hook' do | ||
expect_no_offenses(<<-RUBY) | ||
before do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts `allow` in `before` hook' do | ||
expect_no_offenses(<<-RUBY) | ||
before do | ||
allow(something).to receive(:foo) | ||
allow_any_instance_of(something).to receive(:foo) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts `expect` in `it`' do | ||
expect_no_offenses(<<-RUBY) | ||
it do | ||
expect(something).to eq('foo') | ||
expect_any_instance_of(something).to receive(:foo) | ||
end | ||
RUBY | ||
end | ||
end |