1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-03 17:55:52 +03:00

Testing: run multiple dispatches on same instance

Этот коммит содержится в:
Max Melentiev
2016-03-04 20:08:25 +06:00
родитель 32b6794924
Коммит e767a65faa
8 изменённых файлов: 100 добавлений и 31 удалений

Просмотреть файл

@@ -1,23 +1,29 @@
require 'telegram/bot/updates_controller/testing'
RSpec.shared_context 'telegram/bot/updates_controller' do
let(:controller_class) { described_class }
let(:instance) { controller_class.new(bot, update) }
let(:controller) do
controller_class.new(bot, update).tap do |x|
x.extend Telegram::Bot::UpdatesController::Testing
end
end
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) do
session = Telegram::Bot::UpdatesController::Session::TestSessionHash.new
allow_any_instance_of(controller_class).to receive(:session) { session }
session
let(:session) { controller.send(:session) }
def dispatch(bot = self.bot, update = self.update)
controller.dispatch_again(bot, update)
end
def dispatch_message(text, options = {})
payload = build_payload :message, options.merge(text: text)
controller_class.dispatch bot, payload
update = build_update :message, options.merge(text: text)
dispatch bot, update
end
def build_payload(type, content)
def build_update(type, content)
deep_stringify type => content
end

Просмотреть файл

@@ -59,18 +59,6 @@ module Telegram
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
delegate :session_store, to: :config

Просмотреть файл

@@ -0,0 +1,47 @@
module Telegram
module Bot
class UpdatesController
module Testing
IVARS_TO_KEEP = %i(@_session).freeze
# Perform multiple dispatches on same instance.
def dispatch_again(bot = nil, update = nil)
recycle!
initialize(bot, update)
dispatch
end
# Cleans controller between dispatches.
# Seems like there is nothing to clean between requests for now:
# everything will be rewriten with #initialize.
#
# With `full` set to `true` it'll clear all cached instance variables.
def recycle!(full = false)
return unless full
(instance_variables - IVARS_TO_KEEP).each do |ivar|
remove_instance_variable(ivar)
end
end
protected
# Stubs session.
def session
@_session ||= Testing::SessionHash.new
end
class SessionHash < Session::SessionHash
def initialize
@data = {}
@loaded = true
@exists = true
end
alias_method :destroy, :clear
alias_method :load!, :id
alias_method :commit, :id
end
end
end
end
end

Просмотреть файл

@@ -4,7 +4,7 @@ module Telegram
# Include this module to type cast update to Virtus model
# using `telegram-bot-types` gem (install this gem first).
module TypedUpdate
def initialize(bot, update)
def initialize(bot = nil, update = nil)
update = Types::Update.new(update) if update && !update.is_a?(Types::Update)
super
end