-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
respond_with_slack_message.rb
34 lines (24 loc) · 998 Bytes
/
respond_with_slack_message.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
# frozen_string_literal: true
require 'rspec/expectations'
RSpec::Matchers.define :respond_with_slack_message do |expected|
include SlackRubyBot::SpecHelpers
match do |actual|
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new
message_command = SlackRubyBot::Hooks::Message.new
channel, user, message, attachments = parse(actual)
allow(client).to receive(:message) do |options|
@messages ||= []
@messages.push options
end
message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments))
matcher = have_received(:message).once
matcher = matcher.with(hash_including(channel: channel, text: expected)) if channel && expected
expect(client).to matcher
true
end
failure_message do |_actual|
message = "expected to receive message with text: #{expected} once,\n received:"
message += @messages&.any? ? @messages.inspect : 'none'
message
end
end