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_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_gvasgn(node) ⇒ Object



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

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

  name = node.name[1..]
  return unless name.eql?('stdout') || name.eql?('stderr')

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