-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify implementation of RSpec matchers.
- Loading branch information
Showing
6 changed files
with
36 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 8 additions & 12 deletions
20
lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
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 | ||
def client.test_messages | ||
@test_received_messages | ||
end | ||
|
||
def client.say(options = {}) | ||
super | ||
@test_received_messages = @test_received_messages.nil? ? [] : @test_received_messages | ||
@test_received_messages.push options | ||
end | ||
|
||
message_command = SlackRubyBot::Hooks::Message.new | ||
channel, user, message, attachments = parse(actual) | ||
|
||
allow(Giphy).to receive(:random) if defined?(Giphy) | ||
|
||
allow(client).to receive(:message) | ||
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)) | ||
@messages = client.test_messages | ||
expect(client).to have_received(:message).with(hash_including(channel: channel, text: expected)).once | ||
true | ||
end | ||
|
||
failure_message do |_actual| | ||
message = "expected to receive message with text: #{expected} once,\n received:" | ||
message += @messages.count.zero? ? 'No response messages received' : @messages.inspect | ||
message += @messages.count.zero? ? 'none' : @messages.inspect | ||
message | ||
end | ||
end |
23 changes: 11 additions & 12 deletions
23
lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters