A platform for community discussion. Free, open, simple.
DEV: Fix failing chat spec and add unexpected failure indicator (#20299)
Martin Brennanc07f1e442fe1
diff --git a/plugins/chat/app/services/base.rb b/plugins/chat/app/services/base.rbindex 57d9a38abb9..c42fe736f93 100644--- a/plugins/chat/app/services/base.rb+++ b/plugins/chat/app/services/base.rb@@ -197,7 +197,7 @@ module Chatdef call(instance, context)method = instance.method(method_name)args = {}- args = context.to_h unless method.arity.zero?+ args = context.to_h if !method.arity.zero?context[result_key] = Context.buildinstance.instance_exec(**args, &method)end@@ -217,7 +217,7 @@ module Chatclass ModelStep < Stepdef call(instance, context)context[name] = super- raise ArgumentError, "Model not found" unless context[name]+ raise ArgumentError, "Model not found" if !context[name]rescue ArgumentError => exceptioncontext[result_key].fail(exception: exception)context.fail!@@ -227,7 +227,7 @@ module Chat# @!visibility privateclass PolicyStep < Stepdef call(instance, context)- unless super+ if !supercontext[result_key].failcontext.fail!end@@ -250,7 +250,7 @@ module Chatcontract = class_name.new(default_values.merge(context.to_h.slice(*attributes)))context[contract_name] = contractcontext[result_key] = Context.build- unless contract.valid?+ if contract.invalid?context[result_key].fail(errors: contract.errors)context.fail!end@@ -384,7 +384,7 @@ module Chat# private## def save_channel(channel:, **)- # fail!("something went wrong") unless channel.save+ # fail!("something went wrong") if !channel.save# end# @!scope classdiff --git a/plugins/chat/lib/steps_inspector.rb b/plugins/chat/lib/steps_inspector.rbindex 1291a95eaf6..001e7a9f773 100644--- a/plugins/chat/lib/steps_inspector.rb+++ b/plugins/chat/lib/steps_inspector.rb@@ -30,9 +30,7 @@ module Chatenddef emoji- return "❌" if failure?- return "✅" if success?- ""+ "#{result_emoji}#{unexpected_result_emoji}"enddef steps@@ -48,6 +46,21 @@ module Chatdef step_resultresult["result.#{type}.#{name}"]end++ def result_emoji+ return "❌" if failure?+ return "✅" if success?+ ""+ end++ def unexpected_result_emoji+ " ⚠️#{unexpected_result_text}" if step_result.try(:[], "spec.unexpected_result")+ end++ def unexpected_result_text+ return " <= expected to return true but got false instead" if failure?+ " <= expected to return false but got true instead"+ endend# @!visibility privatediff --git a/plugins/chat/spec/lib/steps_inspector_spec.rb b/plugins/chat/spec/lib/steps_inspector_spec.rbindex 1c9574bb00e..4fa9fa5fcb2 100644--- a/plugins/chat/spec/lib/steps_inspector_spec.rb+++ b/plugins/chat/spec/lib/steps_inspector_spec.rb@@ -131,6 +131,47 @@ RSpec.describe Chat::StepsInspector doOUTPUTendend++ context "when running in specs" do+ context "when a successful step is flagged as being an unexpected result" do+ before { result["result.policy.policy"]["spec.unexpected_result"] = true }++ it "adapts its output accordingly" do+ expect(output).to eq <<~OUTPUT.chomp+ [1/7] [model] 'model' ✅+ [2/7] [policy] 'policy' ✅ ⚠️ <= expected to return false but got true instead+ [3/7] [contract] 'default' ✅+ [4/7] [transaction]+ [5/7] [step] 'in_transaction_step_1' ✅+ [6/7] [step] 'in_transaction_step_2' ✅+ [7/7] [step] 'final_step' ✅+ OUTPUT+ end+ end++ context "when a failing step is flagged as being an unexpected result" do+ before do+ class DummyService+ def policy+ false+ end+ end+ result["result.policy.policy"]["spec.unexpected_result"] = true+ end++ it "adapts its output accordingly" do+ expect(output).to eq <<~OUTPUT.chomp+ [1/7] [model] 'model' ✅+ [2/7] [policy] 'policy' ❌ ⚠️ <= expected to return true but got false instead+ [3/7] [contract] 'default'+ [4/7] [transaction]+ [5/7] [step] 'in_transaction_step_1'+ [6/7] [step] 'in_transaction_step_2'+ [7/7] [step] 'final_step'+ OUTPUT+ end+ end+ endenddescribe "#error" dodiff --git a/plugins/chat/spec/services/update_user_last_read_spec.rb b/plugins/chat/spec/services/update_user_last_read_spec.rbindex 40a5b105605..568821fa670 100644--- a/plugins/chat/spec/services/update_user_last_read_spec.rb+++ b/plugins/chat/spec/services/update_user_last_read_spec.rb@@ -65,7 +65,9 @@ RSpec.describe Chat::Service::UpdateUserLastRead docontext "when message doesn’t exist" dobefore do- params[:message_id] = 2+ message = Fabricate(:chat_message)+ params[:message_id] = message.id+ message.trash!membership.update!(last_read_message_id: 1)enddiff --git a/plugins/chat/spec/support/chat_service_matcher.rb b/plugins/chat/spec/support/chat_service_matcher.rbindex a75d1a798c4..1cbfd64df84 100644--- a/plugins/chat/spec/support/chat_service_matcher.rb+++ b/plugins/chat/spec/support/chat_service_matcher.rb@@ -15,6 +15,7 @@ module Chatenddef failure_message+ set_unexpected_resultmessage =if !step_exists?"Expected #{type} '#{name}' (key: '#{step}') was not found in the result object."@@ -27,6 +28,7 @@ module Chatenddef failure_message_when_negated+ set_unexpected_resultmessage = "Expected #{type} '#{name}' (key: '#{step}') to succeed but it failed."error_message_with_inspection(message)end@@ -61,6 +63,11 @@ module Chatinspector = StepsInspector.new(result)"#{message}\n\n#{inspector.inspect}\n\n#{inspector.error}"end++ def set_unexpected_result+ return unless result[step]+ result[step]["spec.unexpected_result"] = true+ endendclass FailContract < FailStep