A platform for community discussion. Free, open, simple.
DEV: Use `run_successfully` matcher in service specs
Loïc Guitautd26d45540e93
diff --git a/app/services/update_site_setting.rb b/app/services/update_site_setting.rbindex be896e4767d..0b4f28ff1f3 100644--- a/app/services/update_site_setting.rb+++ b/app/services/update_site_setting.rb@@ -4,14 +4,10 @@ class UpdateSiteSettinginclude Service::Basepolicy :current_user_is_admin-contract-step :convert_name_to_sym-policy :setting_is_visiblepolicy :setting_is_configurable-step :cleanup_valuestep :savediff --git a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rbindex 3914f37943b..b825e1dae03 100644--- a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb+++ b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb@@ -28,6 +28,8 @@ RSpec.describe Chat::AddUsersToChannel docontext "when all steps pass" dobefore { channel.add(current_user) }+ it { is_expected.to run_successfully }+it "fetches users to add" doexpect(result.target_users.map(&:username)).to contain_exactly(*users.map(&:username))enddiff --git a/plugins/chat/spec/services/chat/auto_join_channel_batch_spec.rb b/plugins/chat/spec/services/chat/auto_join_channel_batch_spec.rbindex 26f47d4ee68..37853130c3e 100644--- a/plugins/chat/spec/services/chat/auto_join_channel_batch_spec.rb+++ b/plugins/chat/spec/services/chat/auto_join_channel_batch_spec.rb@@ -86,6 +86,8 @@ describe Chat::AutoJoinChannelBatch docontext "when more than one membership is created" dolet(:user_ids) { Fabricate.times(2, :user).map(&:id) }+ it { is_expected.to run_successfully }+it "does not recalculate user count" do::Chat::ChannelMembershipManager.any_instance.expects(:recalculate_user_count).neverresult@@ -101,6 +103,8 @@ describe Chat::AutoJoinChannelBatch doendcontext "when only one membership is created" do+ it { is_expected.to run_successfully }+it "recalculates user count" do::Chat::ChannelMembershipManager.any_instance.expects(:recalculate_user_count).onceresultdiff --git a/plugins/chat/spec/services/chat/create_category_channel_spec.rb b/plugins/chat/spec/services/chat/create_category_channel_spec.rbindex ea0fc252a15..5fbc574bbd1 100644--- a/plugins/chat/spec/services/chat/create_category_channel_spec.rb+++ b/plugins/chat/spec/services/chat/create_category_channel_spec.rb@@ -60,6 +60,8 @@ RSpec.describe Chat::CreateCategoryChannel doendcontext "when all steps pass" do+ it { is_expected.to run_successfully }+it "creates the channel" doexpect { result }.to change { Chat::Channel.count }.by(1)expect(result.channel).to have_attributes(diff --git a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rbindex 844247d40d2..0b80c0f6ab8 100644--- a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb+++ b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb@@ -17,6 +17,7 @@ RSpec.describe Chat::CreateDirectMessageChannel doexpect(contract.target_usernames).to eq(%w[lechuck elaine])endend+context "when the target_groups argument is a string" dolet(:params) { { target_groups: "admins,moderators" } }@@ -42,6 +43,8 @@ RSpec.describe Chat::CreateDirectMessageChannel dolet(:params) { { guardian: guardian, target_usernames: target_usernames, name: name } }context "when all steps pass" do+ it { is_expected.to run_successfully }+it "sets the service result as successful" doexpect(result).to be_a_successenddiff --git a/plugins/chat/spec/services/chat/create_message_spec.rb b/plugins/chat/spec/services/chat/create_message_spec.rbindex 51b4172e4ef..5ed68b2e221 100644--- a/plugins/chat/spec/services/chat/create_message_spec.rb+++ b/plugins/chat/spec/services/chat/create_message_spec.rb@@ -237,7 +237,7 @@ RSpec.describe Chat::CreateMessage docontext "when user is a bot" dofab!(:user) { Discourse.system_user }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when membership is enforced" do@@ -248,7 +248,7 @@ RSpec.describe Chat::CreateMessage doparams[:enforce_membership] = trueend- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when user can join channel" do@@ -291,6 +291,8 @@ RSpec.describe Chat::CreateMessage doit_behaves_like "creating a new message"it_behaves_like "a message in a thread"+ it { is_expected.to run_successfully }+it "assigns the thread to the new message" doexpect(message).to have_attributes(in_reply_to: an_object_having_attributes(thread: thread),@@ -312,6 +314,8 @@ RSpec.describe Chat::CreateMessage dolet(:original_user) { reply_to.user }end+ it { is_expected.to run_successfully }+it "creates a new thread" doexpect { result }.to change { Chat::Thread.count }.by(1)expect(message).to have_attributes(@@ -389,6 +393,8 @@ RSpec.describe Chat::CreateMessage doit_behaves_like "creating a new message"it_behaves_like "a message in a thread"+ it { is_expected.to run_successfully }+it "does not publish the thread" doChat::Publisher.expects(:publish_thread_created!).neverresult@@ -400,6 +406,8 @@ RSpec.describe Chat::CreateMessage doit_behaves_like "creating a new message"it_behaves_like "a message in a thread"+ it { is_expected.to run_successfully }+it "does not publish the thread" doChat::Publisher.expects(:publish_thread_created!).neverresult@@ -418,6 +426,8 @@ RSpec.describe Chat::CreateMessage docontext "when message is valid" doit_behaves_like "creating a new message"+ it { is_expected.to run_successfully }+it "updates membership last_read_message attribute" doexpect { result }.to change { membership.reload.last_read_message }enddiff --git a/plugins/chat/spec/services/chat/create_thread_spec.rb b/plugins/chat/spec/services/chat/create_thread_spec.rbindex c7cb6cc9d43..e2479837901 100644--- a/plugins/chat/spec/services/chat/create_thread_spec.rb+++ b/plugins/chat/spec/services/chat/create_thread_spec.rb@@ -25,9 +25,7 @@ RSpec.describe Chat::CreateThread doendcontext "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "creates a thread" doresultdiff --git a/plugins/chat/spec/services/chat/flag_message_spec.rb b/plugins/chat/spec/services/chat/flag_message_spec.rbindex 499c2183053..83b58bebaf1 100644--- a/plugins/chat/spec/services/chat/flag_message_spec.rb+++ b/plugins/chat/spec/services/chat/flag_message_spec.rb@@ -44,6 +44,8 @@ RSpec.describe Chat::FlagMessage docontext "when all steps pass" dofab!(:current_user) { Fabricate(:admin) }+ it { is_expected.to run_successfully }+it "flags the message" doexpect { result }.to change { Reviewable.count }.by(1)diff --git a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rbindex 2dce1cfef0b..0d0dafdcbae 100644--- a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb+++ b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb@@ -29,9 +29,7 @@ RSpec.describe Chat::InviteUsersToChannel doendcontext "when all steps pass" do- it "sets the service result as successful" do- expect(result).to run_service_successfully- end+ it { is_expected.to run_successfully }it "creates the notifications for allowed users" doresultdiff --git a/plugins/chat/spec/services/chat/leave_channel_spec.rb b/plugins/chat/spec/services/chat/leave_channel_spec.rbindex c28d95154de..37b33d21b2f 100644--- a/plugins/chat/spec/services/chat/leave_channel_spec.rb+++ b/plugins/chat/spec/services/chat/leave_channel_spec.rb@@ -28,6 +28,8 @@ RSpec.describe Chat::LeaveChannel doChat::Channel.ensure_consistency!end+ it { is_expected.to run_successfully }+it "unfollows the channel" domembership = channel_1.membership_for(current_user)@@ -40,6 +42,8 @@ RSpec.describe Chat::LeaveChannel doendcontext "with no existing membership" do+ it { is_expected.to run_successfully }+it "does nothing" doexpect { result }.to_not change { Chat::UserChatChannelMembership }end@@ -54,6 +58,8 @@ RSpec.describe Chat::LeaveChannel dobefore { Chat::Channel.ensure_consistency! }+ it { is_expected.to run_successfully }+it "leaves the channel" domembership = channel_1.membership_for(current_user)@@ -71,6 +77,8 @@ RSpec.describe Chat::LeaveChannel doendcontext "with no existing membership" do+ it { is_expected.to run_successfully }+it "does nothing" doexpect { result }.to_not change { Chat::UserChatChannelMembership }end@@ -85,6 +93,8 @@ RSpec.describe Chat::LeaveChannel dobefore { Chat::Channel.ensure_consistency! }+ it { is_expected.to run_successfully }+it "unfollows the channel" domembership = channel_1.membership_for(current_user)@@ -100,6 +110,8 @@ RSpec.describe Chat::LeaveChannel doendcontext "with no existing membership" do+ it { is_expected.to run_successfully }+it "does nothing" doexpect { result }.to_not change { Chat::UserChatChannelMembership }enddiff --git a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_messages_spec.rbindex ce210e7c3aa..ffdad6ec100 100644--- a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_messages_spec.rb@@ -29,6 +29,8 @@ RSpec.describe Chat::ListChannelMessages doendcontext "when channel exists" do+ it { is_expected.to run_successfully }+it "finds the correct channel" doexpect(result.channel).to eq(channel)end@@ -37,6 +39,8 @@ RSpec.describe Chat::ListChannelMessages docontext "when fetch_eventual_membership" docontext "when user has membership" do+ it { is_expected.to run_successfully }+it "finds the correct membership" doexpect(result.membership).to eq(channel.membership_for(user))end@@ -45,6 +49,8 @@ RSpec.describe Chat::ListChannelMessages docontext "when user has no membership" dobefore { channel.membership_for(user).destroy! }+ it { is_expected.to run_successfully }+it "finds no membership" doexpect(result.membership).to be_blankend@@ -86,7 +92,7 @@ RSpec.describe Chat::ListChannelMessages docontext "when target_message_exists" docontext "when no target_message_id is given" do- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when target message is not found" do@@ -99,7 +105,7 @@ RSpec.describe Chat::ListChannelMessages dofab!(:target_message) { Fabricate(:chat_message, chat_channel: channel) }let(:optional_params) { { target_message_id: target_message.id } }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when target message is trashed" do@@ -117,13 +123,13 @@ RSpec.describe Chat::ListChannelMessages docontext "when user is the message creator" dofab!(:target_message) { Fabricate(:chat_message, chat_channel: channel, user: user) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when user is admin" dofab!(:user) { Fabricate(:admin) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendend@@ -132,6 +138,8 @@ RSpec.describe Chat::ListChannelMessages docontext "with no params" dofab!(:messages) { Fabricate.times(20, :chat_message, chat_channel: channel) }+ it { is_expected.to be_a_success }+it "returns messages" doexpect(result.can_load_more_past).to eq(false)expect(result.can_load_more_future).to eq(false)@@ -149,6 +157,8 @@ RSpec.describe Chat::ListChannelMessages dolet(:optional_params) { { target_date: 2.days.ago } }+ it { is_expected.to be_a_success }+it "includes past and future messages" doexpect(result.messages).to eq([past_message, future_message])end@@ -164,6 +174,8 @@ RSpec.describe Chat::ListChannelMessages dothread_1.add(user)end+ it { is_expected.to be_a_success }+it "returns tracking" doFabricate(:chat_message, chat_channel: channel, thread: thread_1)@@ -175,6 +187,8 @@ RSpec.describe Chat::ListChannelMessages docontext "when thread is forced" dobefore { thread_1.update!(force: true) }+ it { is_expected.to be_a_success }+it "returns tracking" doFabricate(:chat_message, chat_channel: channel, thread: thread_1)@@ -193,6 +207,8 @@ RSpec.describe Chat::ListChannelMessages dothread_1.add(user)end+ it { is_expected.to be_a_success }+it "returns tracking" doFabricate(:chat_message, chat_channel: channel, thread: thread_1)diff --git a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rbindex 62ab38b7023..059a93b7f01 100644--- a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb@@ -29,6 +29,8 @@ RSpec.describe Chat::ListChannelThreadMessages doendcontext "when thread exists" do+ it { is_expected.to run_successfully }+it "finds the correct channel" doexpect(result.thread).to eq(thread)end@@ -44,7 +46,7 @@ RSpec.describe Chat::ListChannelThreadMessages docontext "with system user" dofab!(:user) { Discourse.system_user }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendend@@ -80,7 +82,7 @@ RSpec.describe Chat::ListChannelThreadMessages docontext "when target_message_exists" docontext "when no target_message_id is given" do- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when target message is not found" do@@ -95,7 +97,7 @@ RSpec.describe Chat::ListChannelThreadMessages doendlet(:optional_params) { { target_message_id: target_message.id } }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when target message is trashed" do@@ -115,13 +117,13 @@ RSpec.describe Chat::ListChannelThreadMessages doFabricate(:chat_message, chat_channel: thread.channel, thread: thread, user: user)end- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when user is admin" dofab!(:user) { Fabricate(:admin) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendenddiff --git a/plugins/chat/spec/services/chat/list_user_channels_spec.rb b/plugins/chat/spec/services/chat/list_user_channels_spec.rbindex 2a94b39a5ea..8af4914a1e6 100644--- a/plugins/chat/spec/services/chat/list_user_channels_spec.rb+++ b/plugins/chat/spec/services/chat/list_user_channels_spec.rb@@ -11,6 +11,8 @@ RSpec.describe Chat::ListUserChannels dobefore { channel_1.add(current_user) }+ it { is_expected.to run_successfully }+it "returns the structured data" doexpect(result.structured[:post_allowed_category_ids]).to eq(nil)expect(result.structured[:unread_thread_overview]).to eq({})diff --git a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rbindex a0618336903..05507470519 100644--- a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb@@ -125,7 +125,7 @@ RSpec.describe ::Chat::LookupChannelThreads dodescribe "model - threads" dobefore { channel_1.add(current_user) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }it "orders threads by the last reply created_at timestamp" do[diff --git a/plugins/chat/spec/services/chat/lookup_thread_spec.rb b/plugins/chat/spec/services/chat/lookup_thread_spec.rbindex 873d30e0a4f..65d9c190d96 100644--- a/plugins/chat/spec/services/chat/lookup_thread_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_thread_spec.rb@@ -19,9 +19,7 @@ RSpec.describe Chat::LookupThread dolet(:params) { { guardian: guardian, thread_id: thread.id, channel_id: thread.channel_id } }context "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "fetches the thread" doexpect(result.thread).to eq(thread)@@ -60,7 +58,7 @@ RSpec.describe Chat::LookupThread docontext "when thread is forced" dobefore { thread.update!(force: true) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendenddiff --git a/plugins/chat/spec/services/chat/lookup_user_threads_spec.rb b/plugins/chat/spec/services/chat/lookup_user_threads_spec.rbindex 940b715efb8..a20758af42d 100644--- a/plugins/chat/spec/services/chat/lookup_user_threads_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_user_threads_spec.rb@@ -5,6 +5,7 @@ RSpec.describe ::Chat::LookupUserThreads dofab!(:current_user) { Fabricate(:user) }fab!(:channel_1) { Fabricate(:chat_channel, threading_enabled: true) }+ fab!(:thread_1) { Fabricate(:chat_thread, channel: channel_1, with_replies: 1) }let(:guardian) { Guardian.new(current_user) }let(:channel_id) { channel_1.id }@@ -12,15 +13,15 @@ RSpec.describe ::Chat::LookupUserThreads dolet(:offset) { 0 }let(:params) { { guardian: guardian, limit: limit, offset: offset } }- before { channel_1.add(current_user) }+ before do+ channel_1.add(current_user)+ thread_1.add(current_user)+ endcontext "when all steps pass" do- it "returns threads" do- thread_1 =- Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|- thread.add(current_user)- end+ it { is_expected.to run_successfully }+ it "returns threads" doexpect(result.threads).to eq([thread_1])end@@ -65,6 +66,7 @@ RSpec.describe ::Chat::LookupUserThreads doit "can offset" doparams[:offset] = 1+ Chat::Thread.destroy_allthreads =Fabricate.times(2, :chat_thread, channel: channel_1, with_replies: 1)@@ -77,21 +79,16 @@ RSpec.describe ::Chat::LookupUserThreads doit "has a min offset" doparams[:offset] = -99- threads =- Fabricate- .times(2, :chat_thread, channel: channel_1, with_replies: 1)- .each { |thread| thread.add(current_user) }+ Chat::Thread.destroy_all+ Fabricate+ .times(2, :chat_thread, channel: channel_1, with_replies: 1)+ .each { |thread| thread.add(current_user) }# 0 because we sort by last_message.created_at, so the last created thread is the first oneexpect(result.threads.length).to eq(2)endit "fetches tracking" do- thread_1 =- Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|- thread.add(current_user)- end-expect(result.tracking).to eq(::Chat::TrackingStateReportQuery.call(guardian: current_user.guardian,@@ -102,58 +99,39 @@ RSpec.describe ::Chat::LookupUserThreads doendit "fetches memberships" do- thread_1 =- Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|- thread.add(current_user)- end-expect(result.memberships).to eq([thread_1.membership_for(current_user)])endit "fetches participants" do- thread_1 =- Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|- thread.add(current_user)- end-expect(result.participants).to eq(::Chat::ThreadParticipantQuery.call(thread_ids: [thread_1.id]),)endit "builds a load_more_url" do- Fabricate(:chat_thread, channel: channel_1, with_replies: 1).tap do |thread|- thread.add(current_user)- end-expect(result.load_more_url).to eq("/chat/api/me/threads?limit=10&offset=10")endendit "doesn't return threads with no replies" do- thread_1 = Fabricate(:chat_thread, channel: channel_1)- thread_1.add(current_user)+ Fabricate(:chat_thread, channel: channel_1).tap { _1.add(current_user) }- expect(result.threads).to eq([])+ expect(result.threads).to eq([thread_1])endit "doesn't return threads with no membership" do- thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)+ Fabricate(:chat_thread, channel: channel_1, with_replies: 1)- expect(result.threads).to eq([])+ expect(result.threads).to eq([thread_1])endit "doesn't return threads when the channel has not threading enabled" dochannel_1.update!(threading_enabled: false)- thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)- thread_1.add(current_user)expect(result.threads).to eq([])endit "doesn't return muted threads" do- thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)- thread_1.add(current_user)thread_1.membership_for(current_user).update!(notification_level: ::Chat::UserChatThreadMembership.notification_levels[:muted],)@@ -163,16 +141,11 @@ RSpec.describe ::Chat::LookupUserThreads doit "doesn't return threads when the channel it not open" dochannel_1.update!(status: Chat::Channel.statuses[:closed])- thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)- thread_1.add(current_user)expect(result.threads).to eq([])endit "returns threads from muted channels" do- thread_1 = Fabricate(:chat_thread, channel: channel_1, with_replies: 1)- thread_1.add(current_user)-channel_1.membership_for(current_user).update!(muted: true)expect(result.threads).to eq([thread_1])diff --git a/plugins/chat/spec/services/chat/mark_all_user_channels_read_spec.rb b/plugins/chat/spec/services/chat/mark_all_user_channels_read_spec.rbindex 346de0648a1..f7b1643618d 100644--- a/plugins/chat/spec/services/chat/mark_all_user_channels_read_spec.rb+++ b/plugins/chat/spec/services/chat/mark_all_user_channels_read_spec.rb@@ -56,12 +56,10 @@ RSpec.describe Chat::MarkAllUserChannelsRead docontext "when the user has no memberships" dolet(:guardian) { Guardian.new(Fabricate(:user)) }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "returns the updated_memberships in context" do- expect(result.updated_memberships).to eq([])+ expect(result.updated_memberships).to be_emptyendend@@ -96,9 +94,7 @@ RSpec.describe Chat::MarkAllUserChannelsRead do)end- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "updates the last_read_message_ids" doresultdiff --git a/plugins/chat/spec/services/chat/mark_thread_title_prompt_seen_spec.rb b/plugins/chat/spec/services/chat/mark_thread_title_prompt_seen_spec.rbindex 76cb6e9afe2..49b7b28cbff 100644--- a/plugins/chat/spec/services/chat/mark_thread_title_prompt_seen_spec.rb+++ b/plugins/chat/spec/services/chat/mark_thread_title_prompt_seen_spec.rb@@ -23,9 +23,7 @@ RSpec.describe Chat::MarkThreadTitlePromptSeen dobefore { thread.update!(last_message: last_reply) }context "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }context "when the user is a member of the thread" dofab!(:membership) { thread.add(current_user) }diff --git a/plugins/chat/spec/services/chat/restore_message_spec.rb b/plugins/chat/spec/services/chat/restore_message_spec.rbindex e7ce41d715c..3f8e4452f3c 100644--- a/plugins/chat/spec/services/chat/restore_message_spec.rb+++ b/plugins/chat/spec/services/chat/restore_message_spec.rb@@ -41,9 +41,7 @@ RSpec.describe Chat::RestoreMessage doendcontext "when the user has permission to restore" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "restores the message" doresultdiff --git a/plugins/chat/spec/services/chat/search_chatable_spec.rb b/plugins/chat/spec/services/chat/search_chatable_spec.rbindex 116d9d50fe5..943f4723103 100644--- a/plugins/chat/spec/services/chat/search_chatable_spec.rb+++ b/plugins/chat/spec/services/chat/search_chatable_spec.rb@@ -43,9 +43,7 @@ RSpec.describe Chat::SearchChatable doendcontext "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "cleans the term" doparams[:term] = "#bob"diff --git a/plugins/chat/spec/services/chat/stop_message_streaming_spec.rb b/plugins/chat/spec/services/chat/stop_message_streaming_spec.rbindex b3d1e3a3378..2a006a88235 100644--- a/plugins/chat/spec/services/chat/stop_message_streaming_spec.rb+++ b/plugins/chat/spec/services/chat/stop_message_streaming_spec.rb@@ -19,7 +19,7 @@ RSpec.describe Chat::StopMessageStreaming docontext "with valid params" dofab!(:current_user) { Fabricate(:admin) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }it "updates the streaming attribute to false" doexpect { result }.to change { message_1.reload.streaming }.to eq(false)@@ -42,7 +42,7 @@ RSpec.describe Chat::StopMessageStreaming docontext "when the user is a bot" dofab!(:current_user) { Discourse.system_user }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endend@@ -69,7 +69,7 @@ RSpec.describe Chat::StopMessageStreaming dobefore { params[:message_id] = reply.id }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when the OM is not from current user" do@@ -89,13 +89,13 @@ RSpec.describe Chat::StopMessageStreaming docontext "when current user is a bot" dofab!(:current_user) { Discourse.system_user }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when current user is an admin" dofab!(:current_user) { Fabricate(:admin) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendend@@ -112,13 +112,13 @@ RSpec.describe Chat::StopMessageStreaming docontext "when current user is a bot" dofab!(:current_user) { Discourse.system_user }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endcontext "when current user is an admin" dofab!(:current_user) { Fabricate(:admin) }- it { is_expected.to be_a_success }+ it { is_expected.to run_successfully }endendenddiff --git a/plugins/chat/spec/services/chat/trash_channel_spec.rb b/plugins/chat/spec/services/chat/trash_channel_spec.rbindex 120394e9d8b..d5fa1009636 100644--- a/plugins/chat/spec/services/chat/trash_channel_spec.rb+++ b/plugins/chat/spec/services/chat/trash_channel_spec.rb@@ -25,9 +25,7 @@ RSpec.describe(Chat::TrashChannel) docontext "when user is allowed to perform the action" dofab!(:current_user) { Fabricate(:admin) }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "trashes the channel" doexpect(result[:channel]).to be_trasheddiff --git a/plugins/chat/spec/services/chat/trash_message_spec.rb b/plugins/chat/spec/services/chat/trash_message_spec.rbindex a1f6a72c6fb..b7bc327a458 100644--- a/plugins/chat/spec/services/chat/trash_message_spec.rb+++ b/plugins/chat/spec/services/chat/trash_message_spec.rb@@ -34,9 +34,7 @@ RSpec.describe Chat::TrashMessage doendcontext "when the user has permission to delete" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "trashes the message" doresultdiff --git a/plugins/chat/spec/services/chat/trash_messages_spec.rb b/plugins/chat/spec/services/chat/trash_messages_spec.rbindex 034fa33a866..10ac0e19c9d 100644--- a/plugins/chat/spec/services/chat/trash_messages_spec.rb+++ b/plugins/chat/spec/services/chat/trash_messages_spec.rb@@ -40,9 +40,7 @@ RSpec.describe Chat::TrashMessages doendcontext "when the user has permission to delete" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "trashes the messages" doresultdiff --git a/plugins/chat/spec/services/chat/unfollow_channel_spec.rb b/plugins/chat/spec/services/chat/unfollow_channel_spec.rbindex 71aacd317c0..3199852f132 100644--- a/plugins/chat/spec/services/chat/unfollow_channel_spec.rb+++ b/plugins/chat/spec/services/chat/unfollow_channel_spec.rb@@ -24,6 +24,8 @@ RSpec.describe Chat::UnfollowChannel docontext "with existing membership" dobefore { channel_1.add(current_user) }+ it { is_expected.to run_successfully }+it "unfollows the channel" domembership = channel_1.membership_for(current_user)@@ -32,6 +34,8 @@ RSpec.describe Chat::UnfollowChannel doendcontext "with no existing membership" do+ it { is_expected.to run_successfully }+it "does nothing" doexpect { result }.to_not change { Chat::UserChatChannelMembership }enddiff --git a/plugins/chat/spec/services/chat/update_channel_spec.rb b/plugins/chat/spec/services/chat/update_channel_spec.rbindex 92729aa778f..2f6f49cb536 100644--- a/plugins/chat/spec/services/chat/update_channel_spec.rb+++ b/plugins/chat/spec/services/chat/update_channel_spec.rb@@ -31,9 +31,7 @@ RSpec.describe Chat::UpdateChannel do.firstend- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "updates the channel accordingly" doresultdiff --git a/plugins/chat/spec/services/chat/update_channel_status_spec.rb b/plugins/chat/spec/services/chat/update_channel_status_spec.rbindex e5ac9b92a8a..b3ccccec7d6 100644--- a/plugins/chat/spec/services/chat/update_channel_status_spec.rb+++ b/plugins/chat/spec/services/chat/update_channel_status_spec.rb@@ -42,9 +42,7 @@ RSpec.describe(Chat::UpdateChannelStatus) docontext "when status is allowed" dolet(:status) { "closed" }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "changes the status" doresultdiff --git a/plugins/chat/spec/services/chat/update_message_spec.rb b/plugins/chat/spec/services/chat/update_message_spec.rbindex 6b39f5d9aa9..edda8fa1b8e 100644--- a/plugins/chat/spec/services/chat/update_message_spec.rb+++ b/plugins/chat/spec/services/chat/update_message_spec.rb@@ -879,9 +879,7 @@ RSpec.describe Chat::UpdateMessage doendcontext "when all steps pass" do- it "sets the service result as successful" do- expect(result).to run_service_successfully- end+ it { is_expected.to run_successfully }it "updates the message" doexpect(result.message.message).to eq("new")diff --git a/plugins/chat/spec/services/chat/update_thread_notification_settings_spec.rb b/plugins/chat/spec/services/chat/update_thread_notification_settings_spec.rbindex 671dd9d088b..c026504b399 100644--- a/plugins/chat/spec/services/chat/update_thread_notification_settings_spec.rb+++ b/plugins/chat/spec/services/chat/update_thread_notification_settings_spec.rb@@ -29,9 +29,7 @@ RSpec.describe Chat::UpdateThreadNotificationSettings dobefore { thread.update!(last_message: last_reply) }context "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }context "when the user is a member of the thread" dofab!(:membership) { thread.add(current_user) }diff --git a/plugins/chat/spec/services/chat/update_thread_spec.rb b/plugins/chat/spec/services/chat/update_thread_spec.rbindex 6d69765b77e..5be66c740f2 100644--- a/plugins/chat/spec/services/chat/update_thread_spec.rb+++ b/plugins/chat/spec/services/chat/update_thread_spec.rb@@ -19,9 +19,7 @@ RSpec.describe Chat::UpdateThread dolet(:params) { { guardian: guardian, thread_id: thread.id, title: title } }context "when all steps pass" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "updates the title of the thread" doresultdiff --git a/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rbindex 854890c0131..9122305685a 100644--- a/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb+++ b/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb@@ -91,9 +91,7 @@ RSpec.describe Chat::UpdateUserChannelLastRead do)end- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "updates the last_read message id" doexpect { result }.to change { membership.reload.last_read_message_id }.to(message_1.id)diff --git a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rbindex e94a8de922d..5802478dd60 100644--- a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb+++ b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb@@ -55,9 +55,7 @@ RSpec.describe Chat::UpdateUserThreadLastRead doendcontext "when params are valid" do- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "publishes new last read to clients" domessages = MessageBus.track_publish { result }diff --git a/plugins/chat/spec/services/chat/upsert_draft_spec.rb b/plugins/chat/spec/services/chat/upsert_draft_spec.rbindex b4fd12f3286..5727730c255 100644--- a/plugins/chat/spec/services/chat/upsert_draft_spec.rb+++ b/plugins/chat/spec/services/chat/upsert_draft_spec.rb@@ -29,6 +29,8 @@ RSpec.describe Chat::UpsertDraft doendcontext "when all steps pass" do+ it { is_expected.to run_successfully }+it "creates draft if data provided and not existing draft" doparams[:data] = MultiJson.dump(message: "a")diff --git a/spec/services/flags/create_flag_spec.rb b/spec/services/flags/create_flag_spec.rbindex 2a411697d57..620cd9bbce6 100644--- a/spec/services/flags/create_flag_spec.rb+++ b/spec/services/flags/create_flag_spec.rb@@ -69,11 +69,10 @@ RSpec.describe(Flags::CreateFlag) doOpenStruct.new(enabled?: true),)end+after { Flag.destroy_by(name: "custom flag name") }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "creates the flag" doresultdiff --git a/spec/services/flags/destroy_flag_spec.rb b/spec/services/flags/destroy_flag_spec.rbindex cfc2e8b4238..6519e363ddd 100644--- a/spec/services/flags/destroy_flag_spec.rb+++ b/spec/services/flags/destroy_flag_spec.rb@@ -1,10 +1,10 @@# frozen_string_literal: trueRSpec.describe(Flags::DestroyFlag) do- fab!(:flag)-subject(:result) { described_class.call(id: flag.id, guardian: current_user.guardian) }+ fab!(:flag)+after { flag.destroy }context "when user is not allowed to perform the action" do@@ -16,6 +16,8 @@ RSpec.describe(Flags::DestroyFlag) docontext "when user is allowed to perform the action" dofab!(:current_user) { Fabricate(:admin) }+ it { is_expected.to run_successfully }+it "sets the service result as successful" doexpect(result).to be_a_successenddiff --git a/spec/services/flags/reorder_flag_spec.rb b/spec/services/flags/reorder_flag_spec.rbindex 1eff5f97d4a..cf49a1dcb40 100644--- a/spec/services/flags/reorder_flag_spec.rb+++ b/spec/services/flags/reorder_flag_spec.rb@@ -35,9 +35,7 @@ RSpec.describe(Flags::ReorderFlag) dodescribed_class.call(flag_id: flag.id, guardian: current_user.guardian, direction: "down")end- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "moves the flag" doexpect(Flag.order(:position).map(&:name)).to eq(diff --git a/spec/services/flags/toggle_flag_spec.rb b/spec/services/flags/toggle_flag_spec.rbindex 4eba4502b58..fea69c40527 100644--- a/spec/services/flags/toggle_flag_spec.rb+++ b/spec/services/flags/toggle_flag_spec.rb@@ -16,9 +16,7 @@ RSpec.describe(Flags::ToggleFlag) doafter { flag.reload.update!(enabled: true) }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "toggles the flag" doexpect(result[:flag].enabled).to be falsediff --git a/spec/services/flags/update_flag_spec.rb b/spec/services/flags/update_flag_spec.rbindex 8292c05ae45..3fb7faacf15 100644--- a/spec/services/flags/update_flag_spec.rb+++ b/spec/services/flags/update_flag_spec.rb@@ -1,8 +1,6 @@# frozen_string_literal: trueRSpec.describe(Flags::UpdateFlag) do- fab!(:flag)-subject(:result) dodescribed_class.call(id: flag.id,@@ -15,6 +13,8 @@ RSpec.describe(Flags::UpdateFlag) do)end+ fab!(:flag)+after { flag.destroy }let(:name) { "edited custom flag name" }@@ -67,9 +67,7 @@ RSpec.describe(Flags::UpdateFlag) docontext "when user is allowed to perform the action" dofab!(:current_user) { Fabricate(:admin) }- it "sets the service result as successful" do- expect(result).to be_a_success- end+ it { is_expected.to run_successfully }it "updates the flag" doresultdiff --git a/spec/services/update_site_setting_spec.rb b/spec/services/update_site_setting_spec.rbindex 7040b6dea19..fda557efc8f 100644--- a/spec/services/update_site_setting_spec.rb+++ b/spec/services/update_site_setting_spec.rb@@ -1,71 +1,74 @@# frozen_string_literal: true-describe(UpdateSiteSetting) do- fab!(:admin)+RSpec.describe UpdateSiteSetting do+ subject(:result) { described_class.call(params) }++ describe described_class::Contract, type: :model do+ subject(:contract) { described_class.new }- def call_service(name, value, user: admin, allow_changing_hidden: false)- described_class.call(- setting_name: name,- new_value: value,- guardian: user.guardian,- allow_changing_hidden:,- )+ it { is_expected.to validate_presence_of :setting_name }end+ fab!(:admin)+ let(:params) { { setting_name:, new_value:, guardian:, allow_changing_hidden: } }+ let(:setting_name) { :title }+ let(:new_value) { "blah whatever" }+ let(:guardian) { admin.guardian }+ let(:allow_changing_hidden) { false }+context "when setting_name is blank" do- it "fails the service contract" do- expect(call_service(nil, "blah whatever")).to fail_a_contract+ let(:setting_name) { nil }- expect(call_service(:"", "blah whatever")).to fail_a_contract- end+ it { is_expected.to fail_a_contract }endcontext "when a non-admin user tries to change a setting" do- it "fails the current_user_is_admin policy" do- expect(call_service(:title, "some new title", user: Fabricate(:moderator))).to fail_a_policy(- :current_user_is_admin,- )- expect(SiteSetting.title).not_to eq("some new title")- end+ let(:guardian) { Guardian.new }++ it { is_expected.to fail_a_policy(:current_user_is_admin) }endcontext "when the user changes a hidden setting" do+ let(:setting_name) { :max_category_nesting }+ let(:new_value) { 3 }+context "when allow_changing_hidden is false" do- it "fails the setting_is_visible policy" do- expect(call_service(:max_category_nesting, 3)).to fail_a_policy(:setting_is_visible)- expect(SiteSetting.max_category_nesting).not_to eq(3)- end+ it { is_expected.to fail_a_policy(:setting_is_visible) }endcontext "when allow_changing_hidden is true" do+ let(:allow_changing_hidden) { true }++ it { is_expected.to run_successfully }+it "updates the specified setting" do- expect(call_service(:max_category_nesting, 3, allow_changing_hidden: true)).to be_success- expect(SiteSetting.max_category_nesting).to eq(3)+ expect { result }.to change { SiteSetting.max_category_nesting }.to(3)endendendcontext "when the user changes a visible setting" do- it "updates the specified setting" do- expect(call_service(:title, "hello this is title")).to be_success- expect(SiteSetting.title).to eq("hello this is title")- end+ let(:new_value) { "hello this is title" }- it "cleans up the new setting value before using it" do- expect(call_service(:suggested_topics, "308viu")).to be_success- expect(SiteSetting.suggested_topics).to eq(308)+ it { is_expected.to run_successfully }- expect(call_service(:max_image_size_kb, "8zf843")).to be_success- expect(SiteSetting.max_image_size_kb).to eq(8843)+ it "updates the specified setting" do+ expect { result }.to change { SiteSetting.title }.to(new_value)endit "creates an entry in the staff action logs" do- expect do expect(call_service(:max_image_size_kb, 44_543)).to be_success end.to change {- UserHistory.where(- action: UserHistory.actions[:change_site_setting],- subject: "max_image_size_kb",- ).count+ expect { result }.to change {+ UserHistory.where(action: UserHistory.actions[:change_site_setting], subject: "title").count}.by(1)end++ context "when value needs cleanup" do+ let(:setting_name) { :max_image_size_kb }+ let(:new_value) { "8zf843" }++ it "cleans up the new setting value before using it" do+ expect { result }.to change { SiteSetting.max_image_size_kb }.to(8843)+ end+ endendenddiff --git a/spec/support/service_matchers.rb b/spec/support/service_matchers.rbindex 7ef1571af40..d2945b8e353 100644--- a/spec/support/service_matchers.rb+++ b/spec/support/service_matchers.rb@@ -10,8 +10,24 @@ module ServiceMatchersenddef failure_message+ message = "Expected the service to succeed but it failed."+ error_message_with_inspection(message)+ end++ def failure_message_when_negated+ message = "Expected the service to fail but it succeeded."+ error_message_with_inspection(message)+ end++ def description+ "run the service successfully"+ end++ private++ def error_message_with_inspection(message)inspector = StepsInspector.new(result)- "Expected to run the service sucessfully but failed:\n\n#{inspector.inspect}\n\n#{inspector.error}"+ "#{message}\n\n#{inspector.inspect}\n\n#{inspector.error}"endend@@ -137,7 +153,7 @@ module ServiceMatchersFailStep.new(name)end- def run_service_successfully+ def run_successfullyRunServiceSuccessfully.newend