зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-05 18:45:51 +03:00
ClientStub and improved rspec helpers
Этот коммит содержится в:
@@ -8,6 +8,7 @@ module Telegram
|
|||||||
class NotFound < Error; end
|
class NotFound < Error; end
|
||||||
|
|
||||||
autoload :Client, 'telegram/bot/client'
|
autoload :Client, 'telegram/bot/client'
|
||||||
|
autoload :ClientStub, 'telegram/bot/client_stub'
|
||||||
autoload :Middleware, 'telegram/bot/middleware'
|
autoload :Middleware, 'telegram/bot/middleware'
|
||||||
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
||||||
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
||||||
|
|||||||
21
lib/telegram/bot/client_stub.rb
Обычный файл
21
lib/telegram/bot/client_stub.rb
Обычный файл
@@ -0,0 +1,21 @@
|
|||||||
|
module Telegram
|
||||||
|
module Bot
|
||||||
|
# Stubbed client for tests. Saves all requests into #requests hash.
|
||||||
|
class ClientStub < Client
|
||||||
|
attr_reader :requests
|
||||||
|
|
||||||
|
def initialize(username = nil)
|
||||||
|
@username = username
|
||||||
|
reset
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset
|
||||||
|
@requests = Hash.new { |h, k| h[k] = [] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def request(action, body)
|
||||||
|
requests[action.to_sym] << body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,8 +1,31 @@
|
|||||||
RSpec.shared_context 'telegram/bot/updates_controller' do
|
RSpec.shared_context 'telegram/bot/updates_controller' do
|
||||||
let(:instance) { described_class.new(bot, update) }
|
let(:controller_class) { described_class }
|
||||||
|
let(:instance) { controller_class.new(bot, update) }
|
||||||
let(:update) { {payload_type => payload} }
|
let(:update) { {payload_type => payload} }
|
||||||
let(:payload_type) { 'some_type' }
|
let(:payload_type) { 'some_type' }
|
||||||
let(:payload) { double(:payload) }
|
let(:payload) { double(:payload) }
|
||||||
let(:bot) { double(username: bot_name) }
|
let(:bot) { Telegram::Bot::ClientStub.new(bot_name) }
|
||||||
let(:bot_name) { 'bot' }
|
let(:bot_name) { 'bot' }
|
||||||
|
let(:session) do
|
||||||
|
session = Telegram::Bot::UpdatesController::Session::TestSessionHash.new
|
||||||
|
allow_any_instance_of(controller_class).to receive(:session) { session }
|
||||||
|
session
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch_message(text, options = {})
|
||||||
|
payload = build_payload :message, options.merge(text: text)
|
||||||
|
controller_class.dispatch bot, payload
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_payload(type, content)
|
||||||
|
deep_stringify type => content
|
||||||
|
end
|
||||||
|
|
||||||
|
def deep_stringify(input)
|
||||||
|
case input
|
||||||
|
when Array then input.map(&method(__callee__))
|
||||||
|
when Hash then input.transform_keys(&:to_s).transform_values(&method(__callee__))
|
||||||
|
else input
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -59,6 +59,18 @@ module Telegram
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TestSessionHash < SessionHash
|
||||||
|
def initialize
|
||||||
|
@data = {}
|
||||||
|
@loaded = true
|
||||||
|
@exists = true
|
||||||
|
end
|
||||||
|
|
||||||
|
alias_method :destroy, :clear
|
||||||
|
alias_method :load!, :id
|
||||||
|
alias_method :commit, :id
|
||||||
|
end
|
||||||
|
|
||||||
module ConfigMethods
|
module ConfigMethods
|
||||||
delegate :session_store, to: :config
|
delegate :session_store, to: :config
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do
|
RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do
|
||||||
include_context 'telegram/bot/updates_controller'
|
include_context 'telegram/bot/updates_controller'
|
||||||
let(:instance) { controller_class.new(bot, update) }
|
|
||||||
let(:controller_class) do
|
let(:controller_class) do
|
||||||
described_class = self.described_class
|
described_class = self.described_class
|
||||||
Class.new(Telegram::Bot::UpdatesController) do
|
Class.new(Telegram::Bot::UpdatesController) do
|
||||||
|
|||||||
@@ -148,7 +148,6 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
subject { -> { instance.process_action(:action) } }
|
subject { -> { instance.process_action(:action) } }
|
||||||
|
|
||||||
context 'when callbacks are defined' do
|
context 'when callbacks are defined' do
|
||||||
let(:instance) { controller_class.new }
|
|
||||||
let(:controller_class) do
|
let(:controller_class) do
|
||||||
Class.new(described_class) do
|
Class.new(described_class) do
|
||||||
before_action :hook
|
before_action :hook
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user