Skip to content

Commit

Permalink
Simplify implementation of RSpec matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Mar 25, 2019
1 parent 2fcde8a commit 9e7a07a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

RSpec::Matchers.define :not_respond do
match do |actual|
client = if respond_to?(:client)
send(:client)
else
SlackRubyBot::Client.new
end
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new

message_command = SlackRubyBot::Hooks::Message.new
channel, user, message, attachments = parse(actual)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

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
client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new

message_command = SlackRubyBot::Hooks::Message.new
channel, user, message, attachments = parse(actual)
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
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

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)
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

@responses = []
expected.each do |exp|
@responses.push(expect(client).to(have_received(:message).with(hash_including(channel: channel, text: exp)).once))
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)]
message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'none'}\n" unless @responses[expected.index(exp)]
end
message
end
Expand Down
6 changes: 6 additions & 0 deletions spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def app
expect(message: "#{SlackRubyBot.config.user} single message")
.to respond_with_slack_message(/single response/i)
end
it 'correctly reports error' do
expect do
expect(message: "#{SlackRubyBot.config.user} single message")
.to respond_with_slack_message('another response')
end.to raise_error RSpec::Expectations::ExpectationNotMetError, "expected to receive message with text: another response once,\n received:[{:text=>\"single response\", :channel=>\"channel\"}]"
end
it 'respond_with_single_message_using_partial_regex_match' do
expect(message: "#{SlackRubyBot.config.user} single message")
.to respond_with_slack_message(/si[n|N]gle/)
Expand Down
9 changes: 9 additions & 0 deletions spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def app
expect(message: "#{SlackRubyBot.config.user} respond 5 times")
.not_to respond_with_slack_messages(expected_responses)
end
it 'correctly reports error' do
expect do
expected_responses = []
6.times { |i| expected_responses.push("response #{i}") }
expect(message: "#{SlackRubyBot.config.user} respond 5 times")
.to respond_with_slack_messages(expected_responses)
end.to raise_error RSpec::Expectations::ExpectationNotMetError, "Expected text: response 5, got none\n"
end

it 'respond_with_multiple_messages_using_string_matches_with_extra_arg' do
expected_responses = []
5.times { |i| expected_responses.push("response #{i}") }
Expand Down

0 comments on commit 9e7a07a

Please sign in to comment.