module Lita::RSpec::Handler

Extras for RSpec to facilitate testing Lita handlers.

Public Class Methods

included(base) click to toggle source

Sets up the RSpec environment to easily test Lita handlers.

# File lib/lita/rspec/handler.rb, line 17
def included(base)
  base.send(:include, Lita::RSpec)

  prepare_handlers(base)
  prepare_adapter(base)
  prepare_let_blocks(base)
  prepare_subject(base)
end

Private Class Methods

prepare_adapter(base) click to toggle source

Stub Lita.adapters

# File lib/lita/rspec/handler.rb, line 29
def prepare_adapter(base)
  base.class_eval do
    before do
      if Lita.version_3_compatibility_mode?
        Lita.config.robot.adapter = :test
      else
        registry.register_adapter(:test, Lita::Adapters::Test)
        registry.config.robot.adapter = :test
      end
    end
  end
end
prepare_handlers(base) click to toggle source

Stub Lita.handlers.

# File lib/lita/rspec/handler.rb, line 43
def prepare_handlers(base)
  base.class_eval do
    before do
      handlers = Set.new(
        [described_class] + Array(base.metadata[:additional_lita_handlers])
      )

      if Lita.version_3_compatibility_mode?
        allow(Lita).to receive(:handlers).and_return(handlers)
      else
        handlers.each do |handler|
          registry.register_handler(handler)
        end
      end
    end
  end
end
prepare_let_blocks(base) click to toggle source

Create common test objects.

# File lib/lita/rspec/handler.rb, line 62
def prepare_let_blocks(base)
  base.class_eval do
    let(:robot) { Robot.new(registry) }
    let(:source) { Source.new(user: user) }
    let(:user) { User.create("1", name: "Test User") }
  end
end
prepare_subject(base) click to toggle source

Set up a working test subject.

# File lib/lita/rspec/handler.rb, line 71
def prepare_subject(base)
  base.class_eval do
    subject { described_class.new(robot) }
  end
end

Public Instance Methods

http() click to toggle source

Returns a Faraday connection hooked up to the currently running robot's Rack app. @return [Faraday::Connection] The connection. @since 4.0.0

# File lib/lita/rspec/handler.rb, line 111
def http
  begin
    require "rack/test"
  rescue LoadError
    raise LoadError, I18n.t("lita.rspec.rack_test_required")
  end unless Rack.const_defined?(:Test)

  Faraday::Connection.new { |c| c.adapter(:rack, robot.app) }
end
replies() click to toggle source

An array of strings that have been sent by the robot during the course of a test. @return [Array<String>] The replies.

# File lib/lita/rspec/handler.rb, line 80
def replies
  robot.chat_service.sent_messages
end
send_command(body, as: user, from: nil, privately: false) click to toggle source

Sends a “command” message to the robot. @param body [String] The message to send. @param as [Lita::User] The user sending the message. @param from [Lita::Room] The room where the message is received from. @return [void]

# File lib/lita/rspec/handler.rb, line 104
def send_command(body, as: user, from: nil, privately: false)
  send_message("#{robot.mention_name}: #{body}", as: as, from: from, privately: privately)
end
send_message(body, as: user, from: nil, privately: false) click to toggle source

Sends a message to the robot. @param body [String] The message to send. @param as [Lita::User] The user sending the message. @param from [Lita::Room] The room where the message is received from. @return [void]

# File lib/lita/rspec/handler.rb, line 89
def send_message(body, as: user, from: nil, privately: false)
  message = Message.new(
    robot,
    body,
    Source.new(user: as, room: from, private_message: privately)
  )

  robot.receive(message)
end