Module: Lita::RSpec::Handler

Includes:
Matchers::ChatRouteMatcher, Matchers::EventRouteMatcher, Matchers::HTTPRouteMatcher
Defined in:
lib/lita/rspec/handler.rb

Overview

Extras for RSpec to facilitate testing Lita handlers.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Matchers::ChatRouteMatcher

#route_command

Class Method Details

.included(base) ⇒ Object

Sets up the RSpec environment to easily test Lita handlers.



16
17
18
19
20
21
22
23
# File 'lib/lita/rspec/handler.rb', line 16

def included(base)
  base.send(:include, Lita::RSpec)

  prepare_handlers(base)
  prepare_let_blocks(base)
  prepare_subject(base)
  prepare_robot(base)
end

Instance Method Details

#does_not_route(message) ⇒ Matchers::Deprecated Also known as: doesnt_route

Deprecated.

Will be removed in Lita 5.0. Use is_expected.not_to route instead.

Starts a chat routing test chain, asserting that a message should not trigger a route.

Parameters:

  • message (String)

    The message that should not trigger the route.

Returns:



128
129
130
131
132
133
134
135
# File 'lib/lita/rspec/handler.rb', line 128

def does_not_route(message)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "does_not_route",
    new_method: "is_expected.not_to route",
  )
  Matchers::Deprecated.new(self, :route, false, message)
end

#does_not_route_command(message) ⇒ Matchers::Deprecated Also known as: doesnt_route_command

Deprecated.

Will be removed in Lita 5.0. Use is_expected.not_to route_command instead.

Starts a chat routing test chain, asserting that a “command” message should not trigger a route.

Parameters:

  • message (String)

    The message that should not trigger the route.

Returns:



159
160
161
162
163
164
165
166
# File 'lib/lita/rspec/handler.rb', line 159

def does_not_route_command(message)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "does_not_route_command",
    new_method: "is_expected.not_to route_command",
  )
  Matchers::Deprecated.new(self, :route_command, false, message)
end

#does_not_route_event(event_name) ⇒ Matchers::Deprecated Also known as: doesnt_route_event

Deprecated.

Will be removed in Lita 5.0. Use is_expected.not_to route_event instead.

Starts an event subscription test chain, asserting that an event should not trigger the target method.

Parameters:

  • event_name (String, Symbol)

    The name of the event that should not be triggered.

Returns:



229
230
231
232
233
234
235
236
# File 'lib/lita/rspec/handler.rb', line 229

def does_not_route_event(event_name)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "does_not_route_event",
    new_method: "is_expected.not_to route_event",
  )
  Matchers::Deprecated.new(self, :route_event, false, event_name)
end

#does_not_route_http(http_method, path) ⇒ Matchers::Deprecated Also known as: doesnt_route_http

Deprecated.

Will be removed in Lita 5.0. Use is_expected.not_to route_http instead.

Starts an HTTP routing test chain, asserting that a request to the given path with the given HTTP request method will not trigger a route.

Parameters:

  • http_method (Symbol)

    The HTTP request method that should not trigger the route.

  • path (String)

    The path URL component that should not trigger the route.

Returns:



196
197
198
199
200
201
202
203
# File 'lib/lita/rspec/handler.rb', line 196

def does_not_route_http(http_method, path)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "does_not_route_http",
    new_method: "is_expected.not_to route_http",
  )
  Matchers::Deprecated.new(self, :route_http, false, http_method, path)
end

#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



97
98
99
100
101
102
103
104
105
# File 'lib/lita/rspec/handler.rb', line 97

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

#routes(message) ⇒ Matchers::Deprecated

Deprecated.

Will be removed in Lita 5.0. Use is_expected.to route instead.

Starts a chat routing test chain, asserting that a message should trigger a route.

Parameters:

  • message (String)

    The message that should trigger the route.

Returns:



113
114
115
116
117
118
119
120
# File 'lib/lita/rspec/handler.rb', line 113

def routes(message)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "routes",
    new_method: "is_expected.to route",
  )
  Matchers::Deprecated.new(self, :route, true, message)
end

#routes_command(message) ⇒ Matchers::Deprecated

Deprecated.

Will be removed in Lita 5.0. Use is_expected.to route_command instead.

Starts a chat routing test chain, asserting that a “command” message should trigger a route.

Parameters:

  • message (String)

    The message that should trigger the route.

Returns:



144
145
146
147
148
149
150
151
# File 'lib/lita/rspec/handler.rb', line 144

def routes_command(message)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "routes_command",
    new_method: "is_expected.to route_command",
  )
  Matchers::Deprecated.new(self, :route_command, true, message)
end

#routes_event(event_name) ⇒ Matchers::Deprecated

Deprecated.

Will be removed in Lita 5.0. Use is_expected.to route_event instead.

Starts an event subscription test chain, asserting that an event should trigger the target method.

Parameters:

  • event_name (String, Symbol)

    The name of the event that should be triggered.

Returns:



213
214
215
216
217
218
219
220
# File 'lib/lita/rspec/handler.rb', line 213

def routes_event(event_name)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "routes_event",
    new_method: "is_expected.to route_event",
  )
  Matchers::Deprecated.new(self, :route_event, true, event_name)
end

#routes_http(http_method, path) ⇒ Matchers::Deprecated

Deprecated.

Will be removed in Lita 5.0. Use is_expected.to route_http instead.

Starts an HTTP routing test chain, asserting that a request to the given path with the given HTTP request method will trigger a route.

Parameters:

  • http_method (Symbol)

    The HTTP request method that should trigger the route.

  • path (String)

    The path URL component that should trigger the route.

Returns:



178
179
180
181
182
183
184
185
# File 'lib/lita/rspec/handler.rb', line 178

def routes_http(http_method, path)
  STDERR.puts I18n.t(
    "lita.rspec.matcher_deprecated",
    old_method: "routes_http",
    new_method: "is_expected.to route_http",
  )
  Matchers::Deprecated.new(self, :route_http, true, http_method, path)
end

#send_command(body, as: user) ⇒ void

This method returns an undefined value.

Sends a “command” message to the robot.

Parameters:

  • body (String)

    The message to send.

  • as (Lita::User)

    The user sending the message.



90
91
92
# File 'lib/lita/rspec/handler.rb', line 90

def send_command(body, as: user)
  send_message("#{robot.mention_name}: #{body}", as: as)
end

#send_message(body, as: user) ⇒ void

This method returns an undefined value.

Sends a message to the robot.

Parameters:

  • body (String)

    The message to send.

  • as (Lita::User)

    The user sending the message.



76
77
78
79
80
81
82
83
84
# File 'lib/lita/rspec/handler.rb', line 76

def send_message(body, as: user)
  message = if as == user
    Message.new(robot, body, source)
  else
    Message.new(robot, body, Source.new(user: as))
  end

  robot.receive(message)
end