Class: RuboCop::Cop::RSpec::ExpectOutput

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/expect_output.rb

Overview

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

Examples:

# 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

Constant Summary collapse

MSG =
'Use `expect { ... }.to output(...).to_%<name>s` '\
'instead of mutating $%<name>s.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_gvasgn(node) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/rspec/expect_output.rb', line 21

def on_gvasgn(node)
  return unless inside_example_scope?(node)

  # rubocop:disable InternalAffairs/NodeDestructuring
  variable_name, _rhs = *node
  # rubocop:enable InternalAffairs/NodeDestructuring
  name = variable_name[1..-1]
  return unless name.eql?('stdout') || name.eql?('stderr')

  add_offense(node.loc.name, message: format(MSG, name: name))
end