From f055ee422807cf1ca9a725e5839cfc7bdded2693 Mon Sep 17 00:00:00 2001 From: Sheldon Bachstein Date: Fri, 28 Oct 2022 17:13:29 -0400 Subject: [PATCH 1/2] Make analytics event matcher show diff when easy [skip changelog] --- spec/support/fake_analytics.rb | 49 +++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/spec/support/fake_analytics.rb b/spec/support/fake_analytics.rb index bbfa2cbc608..de80ba1992e 100644 --- a/spec/support/fake_analytics.rb +++ b/spec/support/fake_analytics.rb @@ -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) + # 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) + }, + color: RSpec::Matchers.configuration.color?, + ) end end From ec08e9601d19d8b3ee37d72941d2c930fd740501 Mon Sep 17 00:00:00 2001 From: Sheldon Bachstein Date: Mon, 31 Oct 2022 10:19:31 -0400 Subject: [PATCH 2/2] Use do/end instead of braces --- spec/support/fake_analytics.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/fake_analytics.rb b/spec/support/fake_analytics.rb index de80ba1992e..ab55de73911 100644 --- a/spec/support/fake_analytics.rb +++ b/spec/support/fake_analytics.rb @@ -151,9 +151,9 @@ def browser_attributes def differ RSpec::Support::Differ.new( - object_preparer: lambda { |object| + object_preparer: lambda do |object| RSpec::Matchers::Composable.surface_descriptions_in(object) - }, + end, color: RSpec::Matchers.configuration.color?, ) end