forked from slack-ruby/slack-ruby-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
respond_with_error.rb
38 lines (31 loc) · 1.02 KB
/
respond_with_error.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'rspec/expectations'
RSpec::Matchers.define :respond_with_error do |error, error_message|
match do |actual|
client = if respond_to?(:client)
send(:client)
else
SlackRubyBot::Client.new
end
message_command = SlackRubyBot::Hooks::Message.new
channel, user, message = parse(actual)
allow(Giphy).to receive(:random) if defined?(Giphy)
begin
expect do
message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user))
end.to raise_error error, error_message
rescue RSpec::Expectations::ExpectationNotMetError => e
@error_message = e.message
raise e
end
true
end
failure_message do |actual|
_, _, message = parse(actual)
@error_message || "expected for '#{message}' to fail with '#{expected}'"
end
private
def parse(actual)
actual = { message: actual } unless actual.is_a?(Hash)
[actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]]
end
end