class RuboCop::Cop::RSpec::HooksBeforeExamples
Checks for before/around/after hooks that come after an example.
@example
# 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
Constants
- MSG
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/rspec/hooks_before_examples.rb, line 41 def on_block(node) return unless example_group_with_body?(node) check_hooks(node.body) if multiline_block?(node.body) end
Also aliased as: on_numblock
Private Instance Methods
autocorrect(corrector, node, first_example)
click to toggle source
# File lib/rubocop/cop/rspec/hooks_before_examples.rb, line 73 def autocorrect(corrector, node, first_example) RuboCop::RSpec::Corrector::MoveNode.new( node, corrector, processed_source ).move_before(first_example) end
check_hooks(node)
click to toggle source
# File lib/rubocop/cop/rspec/hooks_before_examples.rb, line 55 def check_hooks(node) first_example = find_first_example(node) return unless first_example first_example.right_siblings.each do |sibling| next unless hook?(sibling) msg = format(MSG, hook: sibling.method_name) add_offense(sibling, message: msg) do |corrector| autocorrect(corrector, sibling, first_example) end end end
find_first_example(node)
click to toggle source
# File lib/rubocop/cop/rspec/hooks_before_examples.rb, line 69 def find_first_example(node) node.children.find { |sibling| example_or_group?(sibling) } end
multiline_block?(block)
click to toggle source
# File lib/rubocop/cop/rspec/hooks_before_examples.rb, line 51 def multiline_block?(block) block.begin_type? end