Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions spec/support/fake_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,49 @@ def browser_attributes
end

failure_message do |actual|
<<~MESSAGE
Expected that FakeAnalytics would have received event #{event_name.inspect}
with #{attributes.inspect}.
matching_events = actual.events[event_name]
if matching_events&.length == 1 && attributes.instance_of?(Hash)
# We found one matching event. Let's show the user a diff of the actual and expected
# attributes
expected = attributes
actual = matching_events.first
message = ''
message += "expected: #{expected}\n"
message += " got: #{actual}\n\n"
message += "Diff:#{differ.diff(actual, expected)}"
message
elsif matching_events&.length == 1 && attributes.instance_of?(RSpec::Matchers::BuiltIn::Include)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully a side-effect of this feature is to encourage the use of these matchers for partial matchers, in support of a ticket created a while back to change the default behavior of the hash-based matching.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and at least it'll be easier to implement that ticket now since the diffs of any existing tests that end up broken will be easier to parse

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and at least it'll be easier to implement that ticket now since the diffs of any existing tests that end up broken will be easier to parse

Yep, though realistically the only way I see that ticket actually getting implemented is with a mass-conversion of existing hash assertions to equivalent include(...).

@zachmargolis zachmargolis Oct 31, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we renamed this matcher have_logged_event_including and create a new have_logged_event_exactly and start encouraging that?

Comment thread
aduth marked this conversation as resolved.
# We found one matching event and an `include` matcher. Let's show the user a diff of the
# actual and expected attributes
expected = attributes.expecteds.first
actual_attrs = matching_events.first
actual_compared = actual_attrs.slice(*expected.keys)
actual_ignored = actual_attrs.except(*expected.keys)
message = ''
message += "expected: include #{expected}\n"
message += " got: #{actual_attrs}\n\n"
message += "Diff:#{differ.diff(actual_compared, expected)}\n"
message += "Attributes ignored by the include matcher:#{differ.diff(
actual_ignored, {}
)}"
message
else
<<~MESSAGE
Expected that FakeAnalytics would have received event #{event_name.inspect}
with #{attributes.inspect}.

Events received:
#{actual.events.pretty_inspect}
MESSAGE
end
end

Events received:
#{actual.events.pretty_inspect}
MESSAGE
def differ
RSpec::Support::Differ.new(
object_preparer: lambda { |object|
RSpec::Matchers::Composable.surface_descriptions_in(object)
},
Comment thread
sheldon-b marked this conversation as resolved.
Outdated
color: RSpec::Matchers.configuration.color?,
)
end
end