From ce559fb9ba8535e1131245daf66d329f2338b743 Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Thu, 14 Sep 2017 23:53:59 -0300 Subject: [PATCH 1/9] When testing a command that returns multiple messages, the respond_with_slack_message matcher was failing even if one of the messages received was the correct message. Refactored respond_with_slack_message to allow a check for a single message from multiple responses. Also added respond_with_slack_messages matcher that can take an array of responses to test commands that return multiple messages. Included are rspec tests for both the matchers. README.md has been edited as well to add notes on the new matcher. --- CHANGELOG.md | 1 + README.md | 1 + .../respond_with_slack_message.rb | 3 +- .../respond_with_slack_messages.rb | 30 +++++++++++++++++++ .../rspec/respond_with_slack_message_spec.rb | 30 +++++++++++++++++++ .../rspec/respond_with_slack_messages_spec.rb | 28 +++++++++++++++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb create mode 100644 spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb create mode 100644 spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index edbcc95..27f01b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 0.10.5 (Next) * Refactored `SlackRubyBot::MVC::Controller::Base`, consolidated ivar handling, centralized object allocations and DRYed up the code - [@chuckremes](https://github.com/chuckremes). +* [#157](https://github.com/slack-ruby/slack-ruby-bot/pull/157): Added support for checking multiple responses at once via new `respond_with_slack_messages` matcher, and single response out of multiple responses to `response_with_slack_message` matcher - [@gcraig99](https://github.com/gcraig99) * Your contribution here. ### 0.10.4 (07/05/2017) diff --git a/README.md b/README.md index 9d58bb3..2d41dc5 100644 --- a/README.md +++ b/README.md @@ -547,6 +547,7 @@ Slack-ruby-bot ships with a number of shared RSpec behaviors that can be used in * [behaves like a slack bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/it_behaves_like_a_slack_bot.rb): A bot quacks like a Slack Ruby bot. * [respond with slack message](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb): The bot responds with a message. +* [respond with slack messages](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb): The bot responds with a set of message. * [respond with error](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb): An exception is raised inside a bot command. Require `slack-ruby-bot/rspec` in your `spec_helper.rb` along with the following dependencies in Gemfile. diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb index 7211f50..c525e5d 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb @@ -13,8 +13,9 @@ allow(Giphy).to receive(:random) if defined?(Giphy) - expect(client).to receive(:message).with(channel: channel, text: expected) + allow(client).to receive(:message) message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) + expect(client).to have_received(:message).with(channel: channel, text: expected).once true end diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb new file mode 100644 index 0000000..da06102 --- /dev/null +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -0,0 +1,30 @@ +require 'rspec/expectations' +RSpec::Matchers.define :respond_with_slack_messages do |expected| + match do |actual| + raise ArgumentError, 'respond_with_slack_messages expects an array of ordered responses' unless expected.is_a? Array + 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) + + allow(client).to receive(:message) + message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) + expected.each do |exp| + expect(client).to have_received(:message).with(channel: channel, text: exp).once + end + true + end + +private + + def parse(actual) + actual = { message: actual } unless actual.is_a?(Hash) + [actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] + end +end diff --git a/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb b/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb new file mode 100644 index 0000000..4dcee5b --- /dev/null +++ b/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb @@ -0,0 +1,30 @@ +describe RSpec do + let! :command do + Class.new(SlackRubyBot::Commands::Base) do + command 'single message' do |client, data, _match| + client.say(text: 'single response', channel: data.channel) + end + command 'respond 5 times' do |client, data, _match| + 5.times do |i| + client.say(text: "response #{i}", channel: data.channel) + end + end + end + end + + def app + SlackRubyBot::App.new + end + it 'respond_with_single_message_using_regex_match' do + expect(message: "#{SlackRubyBot.config.user} single message") + .to respond_with_slack_message(/single response/i) + end + it 'respond_with_single_message_using_string_match' do + expect(message: "#{SlackRubyBot.config.user} single message") + .to respond_with_slack_message('single response') + end + it 'respond_with_multiple_messages_looking_for_single_match' do + expect(message: "#{SlackRubyBot.config.user} respond 5 times") + .to respond_with_slack_message('response 1') + end +end diff --git a/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb b/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb new file mode 100644 index 0000000..d329d7b --- /dev/null +++ b/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb @@ -0,0 +1,28 @@ +describe RSpec do + let! :command do + Class.new(SlackRubyBot::Commands::Base) do + command 'respond 5 times' do |client, data, _match| + 5.times do |i| + client.say(text: "response #{i}", channel: data.channel) + end + end + end + end + + def app + SlackRubyBot::App.new + end + + it 'respond_with_multiple_messages_using_regex_matches' do + expected_responses = [] + 5.times { |i| expected_responses.push(/response #{i}/i) } + expect(message: "#{SlackRubyBot.config.user} respond 5 times") + .to respond_with_slack_messages(expected_responses) + end + it 'respond_with_multiple_messages_using_string_matches' do + expected_responses = [] + 5.times { |i| expected_responses.push("response #{i}") } + expect(message: "#{SlackRubyBot.config.user} respond 5 times") + .to respond_with_slack_messages(expected_responses) + end +end From 008b18508e3d3fa773e6c8415b922eae3e0410af Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Fri, 15 Sep 2017 16:36:21 -0300 Subject: [PATCH 2/9] migration_in_progress was failing because the default rtm method was changed from start to connect in the slack-ruby-client. Changed vcr file to reflect rtm.connect. Added better logging to respond_with_slack_message and respond_with_slack_messages so it's easier to see why the test is failing. Added a few more tests for the message expectations. Fixed CHANGELOG.md message as requested. --- CHANGELOG.md | 2 +- .../fixtures/slack/migration_in_progress.yml | 4 ++-- .../respond_with_slack_message.rb | 24 +++++++++++++------ .../respond_with_slack_messages.rb | 20 ++++++++++++---- .../rspec/respond_with_slack_message_spec.rb | 12 ++++++++++ .../rspec/respond_with_slack_messages_spec.rb | 6 +++++ 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27f01b5..d6e0990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ### 0.10.5 (Next) * Refactored `SlackRubyBot::MVC::Controller::Base`, consolidated ivar handling, centralized object allocations and DRYed up the code - [@chuckremes](https://github.com/chuckremes). -* [#157](https://github.com/slack-ruby/slack-ruby-bot/pull/157): Added support for checking multiple responses at once via new `respond_with_slack_messages` matcher, and single response out of multiple responses to `response_with_slack_message` matcher - [@gcraig99](https://github.com/gcraig99) +* [#157](https://github.com/slack-ruby/slack-ruby-bot/pull/157): Added `respond_with_slack_messages` - [@gcraig99](https://github.com/gcraig99). * Your contribution here. ### 0.10.4 (07/05/2017) diff --git a/lib/slack-ruby-bot/rspec/support/fixtures/slack/migration_in_progress.yml b/lib/slack-ruby-bot/rspec/support/fixtures/slack/migration_in_progress.yml index 8a331a1..1d15b98 100644 --- a/lib/slack-ruby-bot/rspec/support/fixtures/slack/migration_in_progress.yml +++ b/lib/slack-ruby-bot/rspec/support/fixtures/slack/migration_in_progress.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: post - uri: https://slack.com/api/rtm.start + uri: https://slack.com/api/rtm.connect body: string: token=token response: @@ -16,7 +16,7 @@ http_interactions: recorded_at: Tue, 28 Apr 2015 12:55:22 GMT - request: method: post - uri: https://slack.com/api/rtm.start + uri: https://slack.com/api/rtm.connect body: string: token=token response: diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb index c525e5d..af61897 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb @@ -1,12 +1,16 @@ require 'rspec/expectations' - RSpec::Matchers.define :respond_with_slack_message do |expected| match do |actual| - client = if respond_to?(:client) - send(:client) - else - SlackRubyBot::Client.new - end + 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 += "#{options.inspect}\n" + end message_command = SlackRubyBot::Hooks::Message.new channel, user, message = parse(actual) @@ -15,10 +19,16 @@ allow(client).to receive(:message) message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) - expect(client).to have_received(:message).with(channel: channel, text: expected).once + @messages = client.test_messages + expect(client).to have_received(:message).with(channel: channel, text: expected).once, -> { return } true end + failure_message do |_actual| + message = "expected to receive message with text: #{expected} once, received #{@messages}" + message + end + private def parse(actual) diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index da06102..5c70b33 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -2,11 +2,16 @@ RSpec::Matchers.define :respond_with_slack_messages do |expected| match do |actual| raise ArgumentError, 'respond_with_slack_messages expects an array of ordered responses' unless expected.is_a? Array - client = if respond_to?(:client) - send(:client) - else - SlackRubyBot::Client.new - end + 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 += "#{options.inspect}\n" + end message_command = SlackRubyBot::Hooks::Message.new channel, user, message = parse(actual) @@ -15,11 +20,16 @@ allow(client).to receive(:message) message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) + @messages = client.test_messages expected.each do |exp| expect(client).to have_received(:message).with(channel: channel, text: exp).once end true end + failure_message do |_actual| + message = "expected to receive messages in order with text: #{expected.join("\n")} got: #{@messages}" + message + end private diff --git a/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb b/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb index 4dcee5b..ac5551b 100644 --- a/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb +++ b/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb @@ -19,6 +19,14 @@ def app expect(message: "#{SlackRubyBot.config.user} single message") .to respond_with_slack_message(/single response/i) 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/) + end + it 'not_respond_with_single_message_using_bad_regex_match' do + expect(message: "#{SlackRubyBot.config.user} single message") + .not_to respond_with_slack_message(/si[g|G]gle/) + end it 'respond_with_single_message_using_string_match' do expect(message: "#{SlackRubyBot.config.user} single message") .to respond_with_slack_message('single response') @@ -27,4 +35,8 @@ def app expect(message: "#{SlackRubyBot.config.user} respond 5 times") .to respond_with_slack_message('response 1') end + it 'respond_with_multiple_messages_no_match' do + expect(message: "#{SlackRubyBot.config.user} respond 5 times") + .not_to respond_with_slack_message('response 7') + end end diff --git a/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb b/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb index d329d7b..ed1cd87 100644 --- a/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb +++ b/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb @@ -25,4 +25,10 @@ def app expect(message: "#{SlackRubyBot.config.user} respond 5 times") .to respond_with_slack_messages(expected_responses) end + it 'not_respond_with_multiple_messages_using_string_matches' do + expected_responses = [] + 6.times { |i| expected_responses.push("response #{i}") } + expect(message: "#{SlackRubyBot.config.user} respond 5 times") + .not_to respond_with_slack_messages(expected_responses) + end end From a39cdfc1afb79d3f98afcebd65f72a0684bca3ac Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Fri, 15 Sep 2017 16:39:33 -0300 Subject: [PATCH 3/9] Fixed typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d41dc5..74b3527 100644 --- a/README.md +++ b/README.md @@ -547,7 +547,7 @@ Slack-ruby-bot ships with a number of shared RSpec behaviors that can be used in * [behaves like a slack bot](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/it_behaves_like_a_slack_bot.rb): A bot quacks like a Slack Ruby bot. * [respond with slack message](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb): The bot responds with a message. -* [respond with slack messages](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb): The bot responds with a set of message. +* [respond with slack messages](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb): The bot responds with a multiple messages. * [respond with error](lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb): An exception is raised inside a bot command. Require `slack-ruby-bot/rspec` in your `spec_helper.rb` along with the following dependencies in Gemfile. From 2d8c3fb5d98df21b2a76e296b7d3f97285bb9eb5 Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Fri, 15 Sep 2017 16:53:49 -0300 Subject: [PATCH 4/9] Incorporated additional feedback including: moving common code in expectations to a module and including it. Checking that expected responds to each, instead of testing specifically for an array. --- .../slack-ruby-bot/respond_with_slack_message.rb | 8 +------- .../slack-ruby-bot/respond_with_slack_messages.rb | 10 ++-------- lib/slack-ruby-bot/rspec/support/spec_helpers.rb | 10 ++++++++++ 3 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 lib/slack-ruby-bot/rspec/support/spec_helpers.rb diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb index af61897..9cfec9d 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb @@ -1,5 +1,6 @@ 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 @@ -28,11 +29,4 @@ def client.say(options = {}) message = "expected to receive message with text: #{expected} once, received #{@messages}" message end - - private - - def parse(actual) - actual = { message: actual } unless actual.is_a?(Hash) - [actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] - end end diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index 5c70b33..22462b0 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -1,7 +1,8 @@ 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.is_a? Array + 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 @@ -30,11 +31,4 @@ def client.say(options = {}) message = "expected to receive messages in order with text: #{expected.join("\n")} got: #{@messages}" message end - -private - - def parse(actual) - actual = { message: actual } unless actual.is_a?(Hash) - [actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] - end end diff --git a/lib/slack-ruby-bot/rspec/support/spec_helpers.rb b/lib/slack-ruby-bot/rspec/support/spec_helpers.rb new file mode 100644 index 0000000..fd10b31 --- /dev/null +++ b/lib/slack-ruby-bot/rspec/support/spec_helpers.rb @@ -0,0 +1,10 @@ +module SlackRubyBot + module SpecHelpers + private + + def parse(actual) + actual = { message: actual } unless actual.is_a?(Hash) + [actual[:channel] || 'channel', actual[:user] || 'user', actual[:message]] + end + end +end From ab2e3843f11dcf7422bb89b5f18b7c82226c528f Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Fri, 15 Sep 2017 16:56:51 -0300 Subject: [PATCH 5/9] Trailing space in CHANGELOG.md flagged by Danger. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6e0990..928687a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ### 0.10.5 (Next) * Refactored `SlackRubyBot::MVC::Controller::Base`, consolidated ivar handling, centralized object allocations and DRYed up the code - [@chuckremes](https://github.com/chuckremes). -* [#157](https://github.com/slack-ruby/slack-ruby-bot/pull/157): Added `respond_with_slack_messages` - [@gcraig99](https://github.com/gcraig99). +* [#157](https://github.com/slack-ruby/slack-ruby-bot/pull/157): Added `respond_with_slack_messages` expectation - [@gcraig99](https://github.com/gcraig99). * Your contribution here. ### 0.10.4 (07/05/2017) From b351432c7ca01501b7bbaad68a8fb31abcf12246 Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Sun, 17 Sep 2017 19:25:08 -0300 Subject: [PATCH 6/9] Formatted he output of the failure message a bit better. --- .../support/slack-ruby-bot/respond_with_slack_message.rb | 7 ++++--- .../support/slack-ruby-bot/respond_with_slack_messages.rb | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb index 9cfec9d..e58e67d 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb @@ -9,8 +9,8 @@ def client.test_messages def client.say(options = {}) super - @test_received_messages = @test_received_messages.nil? ? '' : @test_received_messages - @test_received_messages += "#{options.inspect}\n" + @test_received_messages = @test_received_messages.nil? ? [] : @test_received_messages + @test_received_messages.push options end message_command = SlackRubyBot::Hooks::Message.new @@ -26,7 +26,8 @@ def client.say(options = {}) end failure_message do |_actual| - message = "expected to receive message with text: #{expected} once, received #{@messages}" + message = "expected to receive message with text: #{expected} once,\n received:" + message += @messages.count.zero? ? 'No response messages received' : @messages.inspect message end end diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index 22462b0..8f11945 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -10,8 +10,8 @@ def client.test_messages def client.say(options = {}) super - @test_received_messages = @test_received_messages.nil? ? '' : @test_received_messages - @test_received_messages += "#{options.inspect}\n" + @test_received_messages = @test_received_messages.nil? ? [] : @test_received_messages + @test_received_messages.push options end message_command = SlackRubyBot::Hooks::Message.new @@ -28,7 +28,8 @@ def client.say(options = {}) true end failure_message do |_actual| - message = "expected to receive messages in order with text: #{expected.join("\n")} got: #{@messages}" + message = "expected to receive messages in order with text: #{expected}\n received:" + message += @messages.count.zero? ? 'No response messages received' : @messages.inspect message end end From 887980d13459c68346a32e5f9fadb5834848e786 Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Sun, 17 Sep 2017 21:09:46 -0300 Subject: [PATCH 7/9] Formatted he output of the failure message a bit better. --- .../support/slack-ruby-bot/respond_with_slack_messages.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index 8f11945..432198f 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -28,8 +28,10 @@ def client.say(options = {}) true end failure_message do |_actual| - message = "expected to receive messages in order with text: #{expected}\n received:" - message += @messages.count.zero? ? 'No response messages received' : @messages.inspect + message = '' + expected.each do |exp| + message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'No Response'}" + end message end end From 7d4bba9463be5976ab8fc060b04afa7a70bb85bc Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Sun, 17 Sep 2017 21:11:51 -0300 Subject: [PATCH 8/9] Formatted he output of the failure message a bit better. --- .../rspec/support/slack-ruby-bot/respond_with_slack_messages.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index 432198f..e51556b 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -30,7 +30,7 @@ def client.say(options = {}) failure_message do |_actual| message = '' expected.each do |exp| - message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'No Response'}" + message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'No Response'}\n" end message end From 5f47a2585229e10b1ce190256108834bbfca78c5 Mon Sep 17 00:00:00 2001 From: Gordon Craig Date: Mon, 18 Sep 2017 07:26:17 -0300 Subject: [PATCH 9/9] Added logic so that the custom failure message only shows the true expectation failure along side the received message. --- .../support/slack-ruby-bot/respond_with_slack_message.rb | 2 +- .../support/slack-ruby-bot/respond_with_slack_messages.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb index e58e67d..d79c6d0 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb @@ -21,7 +21,7 @@ def client.say(options = {}) allow(client).to receive(:message) message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) @messages = client.test_messages - expect(client).to have_received(:message).with(channel: channel, text: expected).once, -> { return } + expect(client).to have_received(:message).with(channel: channel, text: expected).once true end diff --git a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb index e51556b..cec7fb6 100644 --- a/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +++ b/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb @@ -22,15 +22,16 @@ def client.say(options = {}) allow(client).to receive(:message) message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user)) @messages = client.test_messages + @responses = [] expected.each do |exp| - expect(client).to have_received(:message).with(channel: channel, text: exp).once + @responses.push(expect(client).to(have_received(:message).with(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" + message += "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'No Response'}\n" unless @responses[expected.index(exp)] end message end