Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve implicit description for RSpec::Rails::Matchers::ActionCable::HaveBroadcastedTo #2795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rspec/rails/matchers/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module RSpec
module Rails
module Matchers
extend RSpec::Matchers::DSL

# Namespace for various implementations of ActionCable features
#
# @api private
Expand Down Expand Up @@ -50,7 +52,10 @@ def have_broadcasted_to(target = nil)

ActionCable::HaveBroadcastedTo.new(target, channel: described_class)
end
alias_method :broadcast_to, :have_broadcasted_to

alias_matcher :broadcast_to, :have_broadcasted_to do |desc|
desc.gsub("have broadcasted", "broadcast")
end

private

Expand Down
13 changes: 10 additions & 3 deletions lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def thrice
exactly(:thrice)
end

def description
"have broadcasted #{base_description}"
end

def failure_message
"expected to broadcast #{base_message}".tap do |msg|
if @unmatching_msgs.any?
Expand Down Expand Up @@ -140,18 +144,21 @@ def set_expected_number(relativity, count)
end
end

def base_message
def base_description
"#{message_expectation_modifier} #{@expected_number} messages to #{stream}".tap do |msg|
msg << " with #{data_description(@data)}" unless @data.nil?
msg << ", but broadcast #{@matching_msgs_count}"
end
end

def base_message
"#{base_description}, but broadcast #{@matching_msgs_count}"
end

def data_description(data)
if data.is_a?(RSpec::Matchers::Composable)
data.description
else
data
data.inspect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did this actually come from, I'm on the fence wether letting user values user to_s is fine or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect a matcher to use this if mentioning an object. The default description for the eq matcher does this:

it { expect("foo").to eq("foo") }

Resultant description:

is expected to eq "foo"

This adds some clarity to what the object is in context.

end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,33 @@ def broadcast(stream, msg)
end
end
end

it "has an appropriate description" do
expect(have_broadcasted_to("my_stream").description).to eq("have broadcasted exactly 1 messages to my_stream")
end

it "has an appropriate description when aliased" do
expect(broadcast_to("my_stream").description).to eq("broadcast exactly 1 messages to my_stream")
end

it "has an appropriate description when stream name is passed as an array" do
expect(have_broadcasted_to(%w[my_stream stream_2]).from_channel(channel).description).to eq("have broadcasted exactly 1 messages to broadcast:my_stream:stream_2")
end

it "has an appropriate description not mentioning the channel when qualified with `#from_channel`" do
expect(have_broadcasted_to("my_stream").from_channel(channel).description).to eq("have broadcasted exactly 1 messages to my_stream")
end
boardfish marked this conversation as resolved.
Show resolved Hide resolved
boardfish marked this conversation as resolved.
Show resolved Hide resolved

it "has an appropriate description including the expected contents when qualified with `#with`" do
expect(have_broadcasted_to("my_stream").from_channel(channel).with("hello world").description).to eq("have broadcasted exactly 1 messages to my_stream with \"hello world\"")
end
it "has an appropriate description including the matcher's description when qualified with `#with` and a composable matcher" do
expect(
have_broadcasted_to("my_stream")
.from_channel(channel)
.with(a_hash_including(a: :b))
.description
).to eq("have broadcasted exactly 1 messages to my_stream with a hash including {:a => :b}")
end
end
end