Sha256: 7cd53fa61fba43418e73f6cb31677fe0afe616eb47e552330855ab5035ab528b

Contents?: true

Size: 1.6 KB

Versions: 12

Compression:

Stored size: 1.6 KB

Contents

require 'telegram/bot/updates_controller/testing'

RSpec.shared_context 'telegram/bot/updates_controller' do
  let(:controller_class) { described_class }
  let(:controller) do
    controller_class.new(bot, update).tap do |x|
      x.extend Telegram::Bot::UpdatesController::Testing
    end
  end
  let(:update) { build_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
  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

  # 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|
  if config.respond_to?(:include_context)
    config.include_context 'telegram/bot/updates_controller', type: :telegram_bot_controller
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
telegram-bot-0.13.0 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.12.4 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.12.3 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.12.1 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.12.0 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.11.3 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.11.2 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.11.1 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.11.0 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.10.2 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.10.1 lib/telegram/bot/updates_controller/rspec_helpers.rb
telegram-bot-0.10.0 lib/telegram/bot/updates_controller/rspec_helpers.rb