Module: RSpec::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/rspec/helpers.rb,
lib/rspec/helpers/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.1.0"

Instance Method Summary collapse

Instance Method Details

#capture_io(&block) ⇒ Object

Wrap around ‘silence_io` making it easy to capture IO. block - the code you wish to run.



36
37
38
# File 'lib/rspec/helpers.rb', line 36

def capture_io(&block)
  silence_io(:capture => true, &block)
end

#silence_io(capture: false) ⇒ Object

capture - whether or not to capture it and return. Silence the output of any method or command being ran. Return nil, Hash { :stderr, :stdout }



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspec/helpers.rb', line 14

def silence_io(capture: false)
  _stdout = $stdout # OG
  _stderr = $stderr # OG
  $stdout = StringIO.new
  $stderr = StringIO.new

  if !capture
    yield
  else
    yield
    return {
      :stderr => $stderr.string,
      :stdout => $stdout.string
    }
  end
ensure
  $stdout = _stdout
  $stderr = _stderr
end