зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 17:55:52 +03:00
Make integration & controller specs consistent
Этот коммит содержится в:
46
lib/telegram/bot/rspec/callback_query_helpers.rb
Обычный файл
46
lib/telegram/bot/rspec/callback_query_helpers.rb
Обычный файл
@@ -0,0 +1,46 @@
|
||||
require 'telegram/bot/rspec'
|
||||
require 'telegram/bot/rspec/message_helpers'
|
||||
|
||||
# Shared helpers for testing callback query updates.
|
||||
RSpec.shared_context 'telegram/bot/callback_query' do
|
||||
include_context 'telegram/bot/integration'
|
||||
include_context 'telegram/bot/message_helpers'
|
||||
|
||||
subject { -> { dispatch callback_query: payload } }
|
||||
let(:payload) { {id: callback_query_id, from: from, message: message, data: data} }
|
||||
let(:callback_query_id) { 11 }
|
||||
let(:message_id) { 22 }
|
||||
let(:message) { {message_id: message_id, chat: chat, text: 'message text'} }
|
||||
let(:data) { raise '`let(:data) { "callback query data here" }` is required' }
|
||||
|
||||
# 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|
|
||||
if config.respond_to?(:include_context)
|
||||
config.include_context 'telegram/bot/callback_query', :telegram_bot, :callback_query
|
||||
end
|
||||
end
|
||||
@@ -1,10 +1,11 @@
|
||||
require 'telegram/bot/rspec/message_helpers'
|
||||
require 'telegram/bot/rspec/callback_query_helpers'
|
||||
|
||||
RSpec.shared_context 'telegram/bot/integration' do
|
||||
include Telegram::Bot::RSpec::ClientMatchers
|
||||
include_context 'telegram/bot/message_helpers'
|
||||
|
||||
let(:bot) { Telegram.bot }
|
||||
let(:default_message_options) { {from: from, chat: chat} }
|
||||
let(:from) { {id: from_id} }
|
||||
let(:from_id) { 123 }
|
||||
let(:chat) { {id: chat_id} }
|
||||
let(:chat_id) { 456 }
|
||||
let(:controller_path) do
|
||||
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
||||
Rails.application.routes.url_helpers.public_send("#{route_name}_path")
|
||||
@@ -18,8 +19,7 @@ RSpec.shared_context 'telegram/bot/integration' do
|
||||
let(:clear_session?) { described_class.respond_to?(:session_store) }
|
||||
before { described_class.session_store.try!(:clear) if clear_session? }
|
||||
|
||||
include Telegram::Bot::RSpec::ClientMatchers
|
||||
|
||||
# Process update.
|
||||
def dispatch(update)
|
||||
if ActionPack::VERSION::MAJOR >= 5
|
||||
post(controller_path, params: update.to_json, headers: request_headers)
|
||||
@@ -27,59 +27,10 @@ RSpec.shared_context 'telegram/bot/integration' do
|
||||
post(controller_path, update.to_json, request_headers)
|
||||
end
|
||||
end
|
||||
|
||||
def dispatch_message(text, options = {})
|
||||
dispatch message: default_message_options.merge(options).merge(text: text)
|
||||
end
|
||||
|
||||
def dispatch_command(*args)
|
||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
dispatch_message("/#{args.join ' '}", options)
|
||||
end
|
||||
|
||||
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
||||
def respond_with_message(expected = Regexp.new(''))
|
||||
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
||||
send_telegram_message(bot, expected, chat_id: chat_id)
|
||||
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|
|
||||
if config.respond_to?(:include_context)
|
||||
config.include_context 'telegram/bot/integration', :telegram_bot
|
||||
config.include_context 'telegram/bot/callback_query', :telegram_bot, :callback_query
|
||||
end
|
||||
end
|
||||
|
||||
26
lib/telegram/bot/rspec/message_helpers.rb
Обычный файл
26
lib/telegram/bot/rspec/message_helpers.rb
Обычный файл
@@ -0,0 +1,26 @@
|
||||
# Shared helpers for testing message updates.
|
||||
RSpec.shared_context 'telegram/bot/message_helpers' do
|
||||
let(:default_message_options) { {from: from, chat: chat} }
|
||||
let(:from) { {id: from_id} }
|
||||
let(:from_id) { 123 }
|
||||
let(:chat) { {id: chat_id} }
|
||||
let(:chat_id) { 456 }
|
||||
|
||||
# Shortcut for dispatching messages with default params.
|
||||
def dispatch_message(text, options = {})
|
||||
dispatch message: default_message_options.merge(options).merge(text: text)
|
||||
end
|
||||
|
||||
# Dispatch command message.
|
||||
def dispatch_command(cmd, *args)
|
||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
args.unshift("/#{cmd}")
|
||||
dispatch_message(args.join(' '), options)
|
||||
end
|
||||
|
||||
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
||||
def respond_with_message(expected = Regexp.new(''))
|
||||
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
||||
send_telegram_message(bot, expected, chat_id: chat_id)
|
||||
end
|
||||
end
|
||||
@@ -1,37 +1,30 @@
|
||||
require 'telegram/bot/updates_controller/testing'
|
||||
require 'telegram/bot/rspec/message_helpers'
|
||||
|
||||
RSpec.shared_context 'telegram/bot/updates_controller' do
|
||||
include Telegram::Bot::RSpec::ClientMatchers
|
||||
include_context 'telegram/bot/message_helpers'
|
||||
|
||||
let(:controller_class) { described_class }
|
||||
let(:controller) do
|
||||
controller_class.new(bot, update).tap do |x|
|
||||
controller_class.new(*controller_args).tap do |x|
|
||||
x.extend Telegram::Bot::UpdatesController::Testing
|
||||
end
|
||||
end
|
||||
let(:update) { build_update(payload_type, payload) }
|
||||
let(:controller_args) { [bot, deep_stringify(update)] }
|
||||
let(:update) { {payload_type => payload} }
|
||||
let(:payload_type) { :some_type }
|
||||
let(:payload) { double(:payload) }
|
||||
let(:bot) { Telegram::Bot::ClientStub.new(bot_name) }
|
||||
let(:bot_name) { 'bot' }
|
||||
let(:session) { controller.send(:session) }
|
||||
let(:from_id) { 123 }
|
||||
let(:chat_id) { 456 }
|
||||
let(:default_message_options) { {from: {id: from_id}, chat: {id: chat_id}} }
|
||||
|
||||
include Telegram::Bot::RSpec::ClientMatchers
|
||||
|
||||
def dispatch(bot = self.bot, update = self.update)
|
||||
controller.dispatch_again(bot, update)
|
||||
end
|
||||
|
||||
def dispatch_message(text, options = {})
|
||||
update = build_update :message, default_message_options.merge(options).merge(text: text)
|
||||
dispatch bot, update
|
||||
end
|
||||
|
||||
def build_update(type, content)
|
||||
deep_stringify type => content
|
||||
# Process update.
|
||||
def dispatch(update = self.update, bot = self.bot)
|
||||
controller.dispatch_again(bot, deep_stringify(update))
|
||||
end
|
||||
|
||||
# Same as `.as_json` but mocks-friendly.
|
||||
def deep_stringify(input)
|
||||
case input
|
||||
when Array then input.map(&method(__callee__))
|
||||
@@ -39,11 +32,6 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
||||
else input
|
||||
end
|
||||
end
|
||||
|
||||
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
||||
def respond_with_message(expected)
|
||||
send_telegram_message(bot, expected, chat_id: chat_id)
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
|
||||
Ссылка в новой задаче
Block a user