Skip to content

Commit 531992c

Browse files
authored
Merge pull request #2795 from boardfish/implicit-description-for-have-broadcasted-to
Improve implicit description for RSpec::Rails::Matchers::ActionCable::HaveBroadcastedTo
2 parents 5cfcf9c + 3e85bd2 commit 531992c

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

lib/rspec/rails/matchers/action_cable.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module RSpec
44
module Rails
55
module Matchers
6+
extend RSpec::Matchers::DSL
7+
68
# Namespace for various implementations of ActionCable features
79
#
810
# @api private
@@ -50,7 +52,10 @@ def have_broadcasted_to(target = nil)
5052

5153
ActionCable::HaveBroadcastedTo.new(target, channel: described_class)
5254
end
53-
alias_method :broadcast_to, :have_broadcasted_to
55+
56+
alias_matcher :broadcast_to, :have_broadcasted_to do |desc|
57+
desc.gsub("have broadcasted", "broadcast")
58+
end
5459

5560
private
5661

lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def thrice
5151
exactly(:thrice)
5252
end
5353

54+
def description
55+
"have broadcasted #{base_description}"
56+
end
57+
5458
def failure_message
5559
"expected to broadcast #{base_message}".tap do |msg|
5660
if @unmatching_msgs.any?
@@ -140,18 +144,21 @@ def set_expected_number(relativity, count)
140144
end
141145
end
142146

143-
def base_message
147+
def base_description
144148
"#{message_expectation_modifier} #{@expected_number} messages to #{stream}".tap do |msg|
145149
msg << " with #{data_description(@data)}" unless @data.nil?
146-
msg << ", but broadcast #{@matching_msgs_count}"
147150
end
148151
end
149152

153+
def base_message
154+
"#{base_description}, but broadcast #{@matching_msgs_count}"
155+
end
156+
150157
def data_description(data)
151158
if data.is_a?(RSpec::Matchers::Composable)
152159
data.description
153160
else
154-
data
161+
data.inspect
155162
end
156163
end
157164

spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,33 @@ def broadcast(stream, msg)
226226
end
227227
end
228228
end
229+
230+
it "has an appropriate description" do
231+
expect(have_broadcasted_to("my_stream").description).to eq("have broadcasted exactly 1 messages to my_stream")
232+
end
233+
234+
it "has an appropriate description when aliased" do
235+
expect(broadcast_to("my_stream").description).to eq("broadcast exactly 1 messages to my_stream")
236+
end
237+
238+
it "has an appropriate description when stream name is passed as an array" do
239+
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")
240+
end
241+
242+
it "has an appropriate description not mentioning the channel when qualified with `#from_channel`" do
243+
expect(have_broadcasted_to("my_stream").from_channel(channel).description).to eq("have broadcasted exactly 1 messages to my_stream")
244+
end
245+
246+
it "has an appropriate description including the expected contents when qualified with `#with`" do
247+
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\"")
248+
end
249+
it "has an appropriate description including the matcher's description when qualified with `#with` and a composable matcher" do
250+
expect(
251+
have_broadcasted_to("my_stream")
252+
.from_channel(channel)
253+
.with(a_hash_including(a: :b))
254+
.description
255+
).to eq("have broadcasted exactly 1 messages to my_stream with a hash including {:a => :b}")
256+
end
229257
end
230258
end

0 commit comments

Comments
 (0)