-
-
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.
Merge pull request #216 from dblock/rspec-matches
Added start_typing RSpec matcher and extended existing matchers to work without arguments
- Loading branch information
Showing
11 changed files
with
233 additions
and
46 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
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
27 changes: 14 additions & 13 deletions
27
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,34 @@ | ||
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 | ||
|
||
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.count.zero? ? 'No response messages received' : @messages.inspect | ||
message += @messages && @messages.any? ? @messages.inspect : 'none' | ||
message | ||
end | ||
end |
43 changes: 25 additions & 18 deletions
43
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
require 'rspec/expectations' | ||
|
||
RSpec::Matchers.define :respond_with_slack_messages do |expected| | ||
include SlackRubyBot::SpecHelpers | ||
|
||
match do |actual| | ||
raise ArgumentError, 'respond_with_slack_messages expects an array of ordered responses' unless expected.respond_to? :each | ||
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new | ||
def client.test_messages | ||
@test_received_messages | ||
end | ||
raise ArgumentError, 'respond_with_slack_messages expects an array of ordered responses' if expected && !expected.respond_to?(:each) | ||
|
||
def client.say(options = {}) | ||
super | ||
@test_received_messages = @test_received_messages.nil? ? [] : @test_received_messages | ||
@test_received_messages.push options | ||
end | ||
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new | ||
|
||
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) | ||
@messages ||= [] | ||
allow(client).to receive(:message) do |options| | ||
@messages.push options | ||
end | ||
|
||
message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments)) | ||
@messages = client.test_messages | ||
|
||
@responses = [] | ||
expected.each do |exp| | ||
@responses.push(expect(client).to(have_received(:message).with(hash_including(channel: channel, text: exp)).once)) | ||
|
||
if expected && expected.any? | ||
expected.each do |exp| | ||
@responses.push(expect(client).to(have_received(:message).with(hash_including(channel: channel, text: exp)).once)) | ||
end | ||
else | ||
expect(@messages.size).to be > 1 | ||
end | ||
|
||
true | ||
end | ||
|
||
failure_message do |_actual| | ||
message = '' | ||
expected.each do |exp| | ||
message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'No Response'}\n" unless @responses[expected.index(exp)] | ||
if expected && expected.any? | ||
expected.map do |exp| | ||
"Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'none'}" unless @responses[expected.index(exp)] | ||
end.compact.join("\n") | ||
else | ||
"Expected to receive multiple messages, got #{@messages.any? ? @messages.size : 'none'}" | ||
end | ||
message | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
lib/slack-ruby-bot/rspec/support/slack-ruby-bot/start_typing.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'rspec/expectations' | ||
|
||
RSpec::Matchers.define :start_typing do |expected| | ||
include SlackRubyBot::SpecHelpers | ||
|
||
match do |actual| | ||
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new | ||
|
||
message_command = SlackRubyBot::Hooks::Message.new | ||
|
||
allow(client).to receive(:typing) do |options| | ||
@test_options = options | ||
end | ||
|
||
channel, user, message, attachments = parse(actual) | ||
message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments)) | ||
|
||
matcher = have_received(:typing).once | ||
matcher = matcher.with(expected) if expected && expected.any? | ||
expect(client).to matcher | ||
|
||
true | ||
end | ||
|
||
failure_message do |_actual| | ||
message = "expected to receive typing with: #{expected} once,\n received:" | ||
message += @test_options && @test_options.any? ? @test_options.inspect : ' none' | ||
message | ||
end | ||
end |
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
Oops, something went wrong.