Module: Lita::RSpec::Handler

Overview

Extras for RSpec to facilitate testing Lita handlers.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Matchers::DeprecatedMethods

#does_not_route, #does_not_route_command, #does_not_route_event, #does_not_route_http, #routes, #routes_command, #routes_event, #routes_http

Methods included from Matchers::ChatRouteMatcher

#route_command

Class Method Details

.included(base) ⇒ Object

Sets up the RSpec environment to easily test Lita handlers.



17
18
19
20
21
22
23
24
# 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

Instance Method Details

#httpFaraday::Connection

Returns a Faraday connection hooked up to the currently running robot’s Rack app.

Returns:

  • (Faraday::Connection)

    The connection.

Since:

  • 4.0.0



111
112
113
114
115
116
117
118
119
# 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

#repliesArray<String>

An array of strings that have been sent by the robot during the course of a test.

Returns:

  • (Array<String>)

    The replies.



80
81
82
# 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) ⇒ void

This method returns an undefined value.

Sends a “command” message to the robot.

Parameters:

  • body (String)

    The message to send.

  • as (Lita::User) (defaults to: user)

    The user sending the message.

  • from (Lita::Room) (defaults to: nil)

    The room where the message is received from.



104
105
106
# 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) ⇒ void

This method returns an undefined value.

Sends a message to the robot.

Parameters:

  • body (String)

    The message to send.

  • as (Lita::User) (defaults to: user)

    The user sending the message.

  • from (Lita::Room) (defaults to: nil)

    The room where the message is received from.



89
90
91
92
93
94
95
96
97
# 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