Sha256: 9d184cdc6806be9a3077e50d8f006845bbe1ed869e64044602a4f78d0ad9557d

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module Telegram
  module Bot
    # Stubbed client for tests. Saves all requests into #requests hash.
    class ClientStub < Client
      attr_reader :requests

      module StubbedConstructor
        def new(*args)
          if self == ClientStub || !ClientStub.stub_all?
            super
          else
            ClientStub.new(args[1])
          end
        end
      end

      class << self
        # Makes all
        def stub_all!(enabled = true)
          Client.extend(StubbedConstructor) unless Client < StubbedConstructor
          return @_stub_all = enabled unless block_given?
          begin
            old = @_stub_all
            stub_all!(enabled)
            yield
          ensure
            stub_all!(old)
          end
        end

        def stub_all?
          @_stub_all
        end
      end

      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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
telegram-bot-0.6.0 lib/telegram/bot/client_stub.rb
telegram-bot-0.5.0 lib/telegram/bot/client_stub.rb