Handle undefined methods with have_attributes matcher

Instead of producing a compilation error on missing/undefind methods, 
substitute in an "undefined" value.
This shows better output to the user.
This commit is contained in:
Michael Miller 2021-09-26 12:50:17 -06:00
parent 20e17851c6
commit 99ced17516
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
2 changed files with 14 additions and 1 deletions

View file

@ -10,6 +10,16 @@ module Spectator::Matchers
# Each key in the tuple is the attribute/method name,
# and the corresponding value is the expected value to match against.
struct AttributesMatcher(ExpectedType) < Matcher
# Stand-in for undefined methods on types.
private module Undefined
extend self
# Text displayed when a method is undefined.
def inspect(io)
io << "<Method undefined>"
end
end
# Expected value and label.
private getter expected
@ -51,7 +61,7 @@ module Spectator::Matchers
{% begin %}
{
{% for attribute in ExpectedType.keys %}
{{attribute}}: object.{{attribute}},
{{attribute}}: object.responds_to?({{attribute.symbolize}}) ? object.{{attribute}} : Undefined,
{% end %}
}
{% end %}