Class: SemanticLogger::Test::RSpec::EventMatcher
- Inherits:
-
Object
- Object
- SemanticLogger::Test::RSpec::EventMatcher
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/semantic_logger/test/rspec.rb
Overview
Matches a single SemanticLogger::Log against a set of expectations.
An expected value may be:
* a Class - the actual value must be a kind_of that class
* :nil - the actual value must be nil
* any value - compared with ==
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(message_includes: nil, payload_includes: nil, exception_includes: nil, **expected) ⇒ EventMatcher
constructor
A new instance of EventMatcher.
- #matches?(event) ⇒ Boolean
Constructor Details
#initialize(message_includes: nil, payload_includes: nil, exception_includes: nil, **expected) ⇒ EventMatcher
Returns a new instance of EventMatcher.
46 47 48 49 50 51 52 53 54 |
# File 'lib/semantic_logger/test/rspec.rb', line 46 def initialize(message_includes: nil, payload_includes: nil, exception_includes: nil, **expected) unknown = expected.keys - EVENT_ATTRIBUTES raise ArgumentError, "Unknown log event attribute(s): #{unknown.join(', ')}" unless unknown.empty? @expected = expected @message_includes = @payload_includes = payload_includes @exception_includes = exception_includes end |
Instance Method Details
#description ⇒ Object
72 73 74 |
# File 'lib/semantic_logger/test/rspec.rb', line 72 def description "be a semantic logger event matching #{full_expectations.inspect}" end |
#failure_message ⇒ Object
76 77 78 79 |
# File 'lib/semantic_logger/test/rspec.rb', line 76 def "expected log event to #{description}, but #{@failure}.\n" \ "Log event: #{event_inspect}" end |
#failure_message_when_negated ⇒ Object
81 82 83 |
# File 'lib/semantic_logger/test/rspec.rb', line 81 def "expected log event not to #{description}.\nLog event: #{event_inspect}" end |
#matches?(event) ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/semantic_logger/test/rspec.rb', line 56 def matches?(event) @event = event return failed("no log event") unless event @expected.each_pair do |name, expected| actual = event.public_send(name) return failed(mismatch(name, expected, actual)) unless attribute_matches?(expected, actual) end return failed(includes_failure(:message, @message_includes)) unless return failed(includes_failure(:payload, @payload_includes)) unless payload_includes_matches? return failed(includes_failure(:exception, @exception_includes)) unless exception_includes_matches? true end |