Module: LogErrorStub
- Defined in:
- lib/exception_handling/log_stub_error.rb
Overview
Test Helper that supports Minitest::Test and Test::Unit Used by tests in the consumers of this gem to track exceptions.
Defined Under Namespace
Classes: ExpectedExceptionNotLogged, UnexpectedExceptionLogged
Instance Attribute Summary collapse
-
#exception_whitelist ⇒ Object
Returns the value of attribute exception_whitelist.
Instance Method Summary collapse
- #clear_exception_whitelist ⇒ Object
-
#exception_filtered?(exception_data) ⇒ Boolean
Did the calling code call expects_exception on this exception?.
-
#expects_exception(pattern, options = {}) ⇒ Object
Call this from your test file to declare what exceptions you expect to raise.
-
#handle_stub_log_error(exception_data, always_raise = false) ⇒ Object
Gets called by ExceptionHandling::log_error in test mode.
-
#is_mini_test? ⇒ Boolean
for overriding when testing this module.
- #setup_log_error_stub ⇒ Object
-
#stub_log_error ⇒ Object
Call this function in your functional tests - usually first line after a “should” statement once called, you can then call expects_exception By stubbing log error, ExceptionHandling will keep a list of all expected exceptions and gracefully note their occurrence.
-
#teardown_log_error_stub ⇒ Object
if used in Minitest::Test this should be called in ‘before teardown` if used in Test::Unit this should be called as the first line of `teardown`.
Instance Attribute Details
#exception_whitelist ⇒ Object
Returns the value of attribute exception_whitelist.
36 37 38 |
# File 'lib/exception_handling/log_stub_error.rb', line 36 def exception_whitelist @exception_whitelist end |
Instance Method Details
#clear_exception_whitelist ⇒ Object
89 90 91 |
# File 'lib/exception_handling/log_stub_error.rb', line 89 def clear_exception_whitelist @exception_whitelist = nil end |
#exception_filtered?(exception_data) ⇒ Boolean
Did the calling code call expects_exception on this exception?
66 67 68 69 70 71 72 73 |
# File 'lib/exception_handling/log_stub_error.rb', line 66 def exception_filtered?(exception_data) @exception_whitelist&.any? do |expectation| if expectation[0] === exception_data[:error] expectation[1][:found] += 1 true end end end |
#expects_exception(pattern, options = {}) ⇒ Object
Call this from your test file to declare what exceptions you expect to raise.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/exception_handling/log_stub_error.rb', line 78 def expects_exception(pattern, = {}) @exception_whitelist ||= [] expected_count = [:count] || 1 = { expected: expected_count, found: 0 } if to_increment = @exception_whitelist.find { |ex| ex[0] == pattern } to_increment[1][:expected] += expected_count else @exception_whitelist << [pattern, ] end end |
#handle_stub_log_error(exception_data, always_raise = false) ⇒ Object
Gets called by ExceptionHandling::log_error in test mode. If you have called expects_exception then this function will simply note that an instance of that exception has occurred - otherwise it will raise (which will generally result in a 500 return code for your test request)
59 60 61 |
# File 'lib/exception_handling/log_stub_error.rb', line 59 def handle_stub_log_error(exception_data, always_raise = false) raise_unexpected_exception(exception_data) if always_raise || !exception_filtered?(exception_data) end |
#is_mini_test? ⇒ Boolean
for overriding when testing this module
39 40 41 |
# File 'lib/exception_handling/log_stub_error.rb', line 39 def is_mini_test? defined?(Minitest::Test) && is_a?(Minitest::Test) end |
#setup_log_error_stub ⇒ Object
12 13 14 15 |
# File 'lib/exception_handling/log_stub_error.rb', line 12 def setup_log_error_stub clear_exception_whitelist stub_log_error unless respond_to?(:dont_stub_log_error) && dont_stub_log_error end |
#stub_log_error ⇒ Object
Call this function in your functional tests - usually first line after a “should” statement once called, you can then call expects_exception By stubbing log error, ExceptionHandling will keep a list of all expected exceptions and gracefully note their occurrence.
49 50 51 |
# File 'lib/exception_handling/log_stub_error.rb', line 49 def stub_log_error ExceptionHandling.stub_handler = self end |
#teardown_log_error_stub ⇒ Object
if used in Minitest::Test this should be called in ‘before teardown` if used in Test::Unit this should be called as the first line of `teardown`
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/exception_handling/log_stub_error.rb', line 19 def teardown_log_error_stub ExceptionHandling.stub_handler = nil return unless @exception_whitelist @exception_whitelist.each do |pattern, match| unless match[:expected] == match[:found] = "log_error expected #{match[:expected]} times with pattern: '#{pattern.is_a?(Regexp) ? pattern.source : pattern}' found #{match[:found]}" if is_mini_test? flunk() else add_failure() end end end end |