From f549ed53a5d021671fc4658ceeccb80a0cd5d4ae Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Sun, 28 Feb 2016 21:53:54 +0600 Subject: [PATCH] ClientStub and improved rspec helpers --- lib/telegram/bot.rb | 1 + lib/telegram/bot/client_stub.rb | 21 +++++++++++++++ .../bot/updates_controller/rspec_helpers.rb | 27 +++++++++++++++++-- .../bot/updates_controller/session.rb | 12 +++++++++ .../updates_controller/typed_update_spec.rb | 1 - spec/telegram/bot/updates_controller_spec.rb | 1 - 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 lib/telegram/bot/client_stub.rb diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb index 1e96985..27ecba5 100644 --- a/lib/telegram/bot.rb +++ b/lib/telegram/bot.rb @@ -8,6 +8,7 @@ module Telegram class NotFound < Error; end autoload :Client, 'telegram/bot/client' + autoload :ClientStub, 'telegram/bot/client_stub' autoload :Middleware, 'telegram/bot/middleware' autoload :UpdatesController, 'telegram/bot/updates_controller' autoload :UpdatesPoller, 'telegram/bot/updates_poller' diff --git a/lib/telegram/bot/client_stub.rb b/lib/telegram/bot/client_stub.rb new file mode 100644 index 0000000..b3b9eb6 --- /dev/null +++ b/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 diff --git a/lib/telegram/bot/updates_controller/rspec_helpers.rb b/lib/telegram/bot/updates_controller/rspec_helpers.rb index ab0ec9c..9cf9a68 100644 --- a/lib/telegram/bot/updates_controller/rspec_helpers.rb +++ b/lib/telegram/bot/updates_controller/rspec_helpers.rb @@ -1,8 +1,31 @@ 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(:payload_type) { 'some_type' } let(:payload) { double(:payload) } - let(:bot) { double(username: bot_name) } + 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 + 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 diff --git a/lib/telegram/bot/updates_controller/session.rb b/lib/telegram/bot/updates_controller/session.rb index 47139d3..0209f98 100644 --- a/lib/telegram/bot/updates_controller/session.rb +++ b/lib/telegram/bot/updates_controller/session.rb @@ -59,6 +59,18 @@ 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 diff --git a/spec/telegram/bot/updates_controller/typed_update_spec.rb b/spec/telegram/bot/updates_controller/typed_update_spec.rb index b7f6874..43ed138 100644 --- a/spec/telegram/bot/updates_controller/typed_update_spec.rb +++ b/spec/telegram/bot/updates_controller/typed_update_spec.rb @@ -1,6 +1,5 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do include_context 'telegram/bot/updates_controller' - let(:instance) { controller_class.new(bot, update) } let(:controller_class) do described_class = self.described_class Class.new(Telegram::Bot::UpdatesController) do diff --git a/spec/telegram/bot/updates_controller_spec.rb b/spec/telegram/bot/updates_controller_spec.rb index a42609d..8a7494b 100644 --- a/spec/telegram/bot/updates_controller_spec.rb +++ b/spec/telegram/bot/updates_controller_spec.rb @@ -148,7 +148,6 @@ RSpec.describe Telegram::Bot::UpdatesController do subject { -> { instance.process_action(:action) } } context 'when callbacks are defined' do - let(:instance) { controller_class.new } let(:controller_class) do Class.new(described_class) do before_action :hook