From 461292fb2bd4135f0a560b64fa24a3f502e8bc3a Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Thu, 7 May 2026 19:58:41 +0700 Subject: [PATCH] test(telegram): fix business-chat send-attachments spec The let(:channel) and the service's message.inbox.channel were different object instances, so the chat_id / business_connection_id stubs on the test instance never applied to the service. Other examples masked it because they only counted requests; the business-chat example matches on body and tripped. Set the values via conversation.additional_attributes (where the channel methods actually read from) instead of stubbing. --- .../telegram/send_attachments_service_spec.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spec/services/telegram/send_attachments_service_spec.rb b/spec/services/telegram/send_attachments_service_spec.rb index f9c625780..14fb22896 100644 --- a/spec/services/telegram/send_attachments_service_spec.rb +++ b/spec/services/telegram/send_attachments_service_spec.rb @@ -3,13 +3,17 @@ require 'rails_helper' RSpec.describe Telegram::SendAttachmentsService do describe '#perform' do let(:channel) { create(:channel_telegram) } - let(:message) { build(:message, conversation: create(:conversation, inbox: channel.inbox)) } + # Channel#chat_id and #business_connection_id read from conversation.additional_attributes, + # so set the values there directly — stubbing the let(:channel) instance doesn't affect + # the service's message.inbox.channel reference (different object instance). + let(:conversation) do + create(:conversation, inbox: channel.inbox, additional_attributes: { 'chat_id' => 'chat123' }) + end + let(:message) { build(:message, conversation: conversation) } let(:service) { described_class.new(message: message) } let(:telegram_api_url) { channel.telegram_api_url } before do - allow(channel).to receive(:chat_id).and_return('chat123') - stub_request(:post, "#{telegram_api_url}/sendMediaGroup") .to_return(status: 200, body: { ok: true, result: [{ message_id: 'media' }] }.to_json, headers: { 'Content-Type' => 'application/json' }) @@ -41,7 +45,11 @@ RSpec.describe Telegram::SendAttachmentsService do end context 'when this is business chat' do - before { allow(channel).to receive(:business_connection_id).and_return('eooW3KF5WB5HxTD7T826') } + before do + conversation.update!(additional_attributes: conversation.additional_attributes.merge( + 'business_connection_id' => 'eooW3KF5WB5HxTD7T826' + )) + end it 'sends all types of attachments in seperate groups and returns the last successful message ID from the batch' do attach_files(message)