Module: Reality::Logging::Assertions

Defined in:
lib/reality/logging.rb

Overview

Module that should be mixed into base test class to test dependent libraries. It is expected that the class this is mixed into supplies a assert_raise method.

Instance Method Summary collapse

Instance Method Details

#assert_logging_error(log_container, expected_message, &block) ⇒ Object

For the specified log container, capture the log output during blocks execution and match specified message. Also ensure an exception is raised with the same message



117
118
119
120
121
122
123
124
# File 'lib/reality/logging.rb', line 117

def assert_logging_error(log_container, expected_message, &block)
  raise 'assert_logging_error called but no block supplied.' unless block_given?
  assert_logging_message(log_container, expected_message) do
    assert_raise(RuntimeError.new(expected_message)) do
      yield block
    end
  end
end

#assert_logging_message(log_container, expected_message, &block) ⇒ Object

For the specified log container, capture the log output during blocks execution and match specified message. A new line is appended to expected_message as the logging system appends one.



104
105
106
107
108
109
110
111
112
# File 'lib/reality/logging.rb', line 104

def assert_logging_message(log_container, expected_message, &block)
  raise 'assert_logging_message called but no block supplied.' unless block_given?

  result = capture_logging(log_container) do
    yield block
  end

  assert_equal "#{expected_message}\n", result
end

#capture_logging(log_container, &block) ⇒ Object

for the specified log container capture the log output during blocks execution and return as a string



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/reality/logging.rb', line 84

def capture_logging(log_container, &block)
  raise 'capture_logging called but no block supplied.' unless block_given?
  logger = log_container.const_get(:Logger)

  logdev = logger.instance_variable_get('@logdev')
  original_dev = logdev.instance_variable_get('@dev')

  capture_io = StringIO.new
  begin
    logdev.instance_variable_set('@dev', capture_io)
    yield block
    return capture_io.string
  ensure
    logdev.instance_variable_set('@dev', original_dev)
  end
end