Module: YetiLogger::TestHelper
- Defined in:
- lib/yeti_logger/test_helper.rb
Overview
Helper methods for tests that interact with the Logging system
Instance Method Summary collapse
-
#expect_to_not_see_log_message(message, level = :debug, &block) ⇒ Object
Execute a block and ensure that the supplied log message is not among the messages logged at the specified level.
-
#expect_to_not_see_log_messages(messages, level = :debug, &block) ⇒ Object
Plural version of above.
-
#expect_to_see_log_message(message, level = :debug, &block) ⇒ Object
Execute a block and ensure that among all the log messages received, message is among them.
-
#expect_to_see_log_messages(messages, level = :debug, &block) ⇒ Object
Plural version of above.
- #get_log_messages(level = :debug, &block) ⇒ Object
- #should_log(level = :info) ⇒ Object
- #should_not_log(level = :info) ⇒ Object
-
#with_log_level(level = Logger::DEBUG) ⇒ Object
Execute a block within the context of a changed log level.
Instance Method Details
#expect_to_not_see_log_message(message, level = :debug, &block) ⇒ Object
Execute a block and ensure that the supplied log message is not among the messages logged at the specified level.
49 50 51 |
# File 'lib/yeti_logger/test_helper.rb', line 49 def (, level = :debug, &block) ([], level, &block) end |
#expect_to_not_see_log_messages(messages, level = :debug, &block) ⇒ Object
Plural version of above
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/yeti_logger/test_helper.rb', line 54 def (, level = :debug, &block) = (level, &block) found = .find do || if .is_a?(Regexp) .find do || =~ end else .include?() end end if found.present? fail "Should not have found #{found.inspect} amongst #{.inspect}" end end |
#expect_to_see_log_message(message, level = :debug, &block) ⇒ Object
Execute a block and ensure that among all the log messages received, message is among them. Not as elegant as a should_log type of method, but I can’t figure out how to inject that into the rspec framework so it’s evaluated after the method is done being called like should_receive’s are.
21 22 23 |
# File 'lib/yeti_logger/test_helper.rb', line 21 def (, level = :debug, &block) ([], level, &block) end |
#expect_to_see_log_messages(messages, level = :debug, &block) ⇒ Object
Plural version of above
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yeti_logger/test_helper.rb', line 26 def (, level = :debug, &block) = (level, &block) # Find each message, removing the first occurrence. .each do || if .is_a?(Regexp) found = .find do || =~ end if found .delete_at(.find_index(found)) else fail "Should have found #{.inspect} amongst #{.inspect}" end else expect().to include() .delete_at(.find_index()) end end end |
#get_log_messages(level = :debug, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/yeti_logger/test_helper.rb', line 72 def (level = :debug, &block) = [] allow(YetiLogger.logger).to receive(level) do |log_line| << log_line end block.call # There is no unstub in rspec 3, but the closest to that would be to # continue to stub it, but defer to the original implementation. allow(YetiLogger.logger).to receive(level).and_call_original end |
#should_log(level = :info) ⇒ Object
88 89 90 |
# File 'lib/yeti_logger/test_helper.rb', line 88 def should_log(level = :info) expect(YetiLogger.logger).to(receive(level)) end |
#should_not_log(level = :info) ⇒ Object
92 93 94 |
# File 'lib/yeti_logger/test_helper.rb', line 92 def should_not_log(level = :info) expect(YetiLogger.logger).to_not(receive(level)) end |
#with_log_level(level = Logger::DEBUG) ⇒ Object
Execute a block within the context of a changed log level. This will ensure the level is returned to is original state upon exit.
7 8 9 10 11 12 13 14 15 |
# File 'lib/yeti_logger/test_helper.rb', line 7 def with_log_level(level = Logger::DEBUG) orig_level = YetiLogger.logger.level begin YetiLogger.logger.level = level yield ensure YetiLogger.logger.level = orig_level end end |