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

Inherits:
Cop
  • 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.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_gvasgn(node) ⇒ Object



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

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

  variable_name, _rhs = *node
  name = variable_name[1..-1]
  return unless name.eql?('stdout') || name.eql?('stderr')

  add_offense(node, location: :name, message: format(MSG, name: name))
end