From 6e4a69b6b1ce6d25ead2b97b1d0df253d4aff753 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Wed, 17 May 2017 17:45:21 +0600 Subject: [PATCH] Fix storing token in ClientStub when config hash is given --- lib/telegram/bot/client_stub.rb | 2 +- spec/integration/requests/named_bot_spec.rb | 9 +++++++++ spec/integration_helper.rb | 19 +++++++++---------- spec/telegram/bot/client_stub_spec.rb | 6 ++++++ 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 spec/integration/requests/named_bot_spec.rb diff --git a/lib/telegram/bot/client_stub.rb b/lib/telegram/bot/client_stub.rb index 0f080fa..8235266 100644 --- a/lib/telegram/bot/client_stub.rb +++ b/lib/telegram/bot/client_stub.rb @@ -35,7 +35,7 @@ module Telegram end def initialize(token = nil, username = nil, **options) - @token = token + @token = token || options[:token] @username = username || options[:username] || token reset end diff --git a/spec/integration/requests/named_bot_spec.rb b/spec/integration/requests/named_bot_spec.rb new file mode 100644 index 0000000..11e2eb2 --- /dev/null +++ b/spec/integration/requests/named_bot_spec.rb @@ -0,0 +1,9 @@ +require 'integration_helper' + +RSpec.describe NamedBotController, :telegram_bot, type: :request do + let(:bot) { Telegram.bots[:named] } + describe '#start' do + subject { -> { dispatch_command :start } } + it { should respond_with_message 'from named' } + end +end diff --git a/spec/integration_helper.rb b/spec/integration_helper.rb index e00682f..9b23fe7 100644 --- a/spec/integration_helper.rb +++ b/spec/integration_helper.rb @@ -18,22 +18,20 @@ class TestApplication < Rails::Application bot: 'default_token', bots: { other: {token: 'other_token'}, + named: {token: 'named_token', username: 'TestBot'}, }, } end Rails.application.initialize! # # Controllers -class DefaultBotController < Telegram::Bot::UpdatesController - def start(*) - respond_with :message, text: 'from default' - end -end - -class OtherBotController < Telegram::Bot::UpdatesController - def start(*) - respond_with :message, text: 'from other' +%w(default other named).each do |bot_name| + controller = Class.new(Telegram::Bot::UpdatesController) do + define_method :start do |*| + respond_with :message, text: "from #{bot_name}" + end end + Object.const_set("#{bot_name}_bot_controller".camelize, controller) end RSpec.configure do |config| @@ -57,7 +55,8 @@ RSpec.configure do |config| extend Telegram::Bot::RoutesHelper telegram_webhooks default: DefaultBotController, - other: OtherBotController + other: OtherBotController, + named: NamedBotController end end end diff --git a/spec/telegram/bot/client_stub_spec.rb b/spec/telegram/bot/client_stub_spec.rb index 56c4c8d..2fe12cd 100644 --- a/spec/telegram/bot/client_stub_spec.rb +++ b/spec/telegram/bot/client_stub_spec.rb @@ -49,5 +49,11 @@ RSpec.describe Telegram::Bot::ClientStub do its(:token) { should eq args[0] } its(:username) { should eq args[1] } end + + context 'when hash config is given' do + let(:args) { [token: 'token', username: 'superbot'] } + its(:token) { should eq args[0][:token] } + its(:username) { should eq args[0][:username] } + end end end