Skip to content

Commit 86db479

Browse files
boardfishJonRowepirj
committed
Improve implicit description for RSpec::Rails::Matchers::ActionCable::HaveBroadcastedTo
Previously, tests defined like this: ```rb it do expect { some_action }.to have_broadcasted_to("stream_name").from_channel(SomeChannel).with("some_data") end ``` would implicitly be given the description `have broadcasted to`, which is derived from the name of the matcher method. This didn't carry any extra information about the data we're expecting to be broadcast from the channel - behaviour which would be very welcome in combination with Turbo Streams. With a matcher for Turbo Streams, we might benefit from a description like "broadcast exactly 1 messages to stream with turbo-stream[action="append"][target="some_list"]". Co-authored-by: Jon Rowe <[email protected]> Co-authored-by: Phil Pirozhkov <[email protected]>
1 parent 38d2b51 commit 86db479

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/rspec/rails/matchers/action_cable.rb

Lines changed: 7 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
@@ -45,12 +47,16 @@ module ActionCable
4547
# expect {
4648
# ActionCable.server.broadcast "messages", text: 'Hi!'
4749
# }.to have_broadcasted_to("messages").with(text: 'Hi!')
50+
4851
def have_broadcasted_to(target = nil)
4952
check_action_cable_adapter
5053

5154
ActionCable::HaveBroadcastedTo.new(target, channel: described_class)
5255
end
53-
alias_method :broadcast_to, :have_broadcasted_to
56+
57+
alias_matcher :broadcast_to, :have_broadcasted_to do |desc|
58+
desc.gsub("have broadcasted", "broadcast")
59+
end
5460

5561
private
5662

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,36 @@ 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+
250+
it { expect("foo").to eq("foo") }
251+
252+
it "has an appropriate description including the matcher's description when qualified with `#with` and a composable matcher" do
253+
expect(
254+
have_broadcasted_to("my_stream")
255+
.from_channel(channel)
256+
.with(a_hash_including(a: :b))
257+
.description
258+
).to eq("have broadcasted exactly 1 messages to my_stream with a hash including {:a => :b}")
259+
end
229260
end
230261
end

0 commit comments

Comments
 (0)