зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-04 18:25:51 +03:00
callback_query context for RSpec
Этот коммит содержится в:
@@ -377,6 +377,13 @@ RSpec.describe TelegramWebhooksController, :telegram_bot do
|
|||||||
subject { -> { dispatch_command :start } }
|
subject { -> { dispatch_command :start } }
|
||||||
it { should respond_with_message 'Hi there!' }
|
it { should respond_with_message 'Hi there!' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# There is context for callback queries with related matchers.
|
||||||
|
describe '#hey_callback_query', :callback_query do
|
||||||
|
let(:data) { "hey:#{name}" }
|
||||||
|
let(:name) { 'Joe' }
|
||||||
|
it { should answer_callback_query('Hey Joe') }
|
||||||
|
it { should edit_current_message :text, text: 'Done' }
|
||||||
end
|
end
|
||||||
|
|
||||||
# For controller specs use
|
# For controller specs use
|
||||||
|
|||||||
@@ -36,14 +36,48 @@ RSpec.shared_context 'telegram/bot/integration' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
||||||
def respond_with_message(expected)
|
def respond_with_message(expected = Regexp.new(''))
|
||||||
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
||||||
send_telegram_message(bot, expected, chat_id: chat_id)
|
send_telegram_message(bot, expected, chat_id: chat_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RSpec.shared_context 'telegram/bot/callback_query', callback_query: true do
|
||||||
|
include_context 'telegram/bot/integration'
|
||||||
|
|
||||||
|
subject { -> { dispatch callback_query: payload } }
|
||||||
|
let(:payload) { {id: 11, from: from, message: message, data: data} }
|
||||||
|
let(:message) { {message_id: 22, chat: chat, text: 'message text'} }
|
||||||
|
|
||||||
|
# Matcher to check that origin message got edited.
|
||||||
|
def edit_current_message(type, options = {})
|
||||||
|
description = 'edit current message'
|
||||||
|
options = options.merge(
|
||||||
|
message_id: message[:message_id],
|
||||||
|
chat_id: chat_id,
|
||||||
|
)
|
||||||
|
Telegram::Bot::RSpec::ClientMatchers::MakeTelegramRequest.new(
|
||||||
|
bot, :"editMessage#{type.to_s.camelize}", description: description
|
||||||
|
).with(hash_including(options))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Matcher to check that callback query is answered.
|
||||||
|
def answer_callback_query(text = Regexp.new(''), options = {})
|
||||||
|
description = "answer callback query with #{text.inspect}"
|
||||||
|
text = a_string_matching(text) if text.is_a?(Regexp)
|
||||||
|
options = options.merge(
|
||||||
|
callback_query_id: payload[:id],
|
||||||
|
text: text,
|
||||||
|
)
|
||||||
|
Telegram::Bot::RSpec::ClientMatchers::MakeTelegramRequest.new(
|
||||||
|
bot, :answerCallbackQuery, description: description
|
||||||
|
).with(hash_including(options))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
if config.respond_to?(:include_context)
|
if config.respond_to?(:include_context)
|
||||||
config.include_context 'telegram/bot/integration', :telegram_bot
|
config.include_context 'telegram/bot/integration', :telegram_bot
|
||||||
|
config.include_context 'telegram/bot/callback_query', :telegram_bot, :callback_query
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user