File tree Expand file tree Collapse file tree 3 files changed +44
-4
lines changed
spec/rspec/rails/matchers/action_cable Expand file tree Collapse file tree 3 files changed +44
-4
lines changed Original file line number Diff line number Diff line change 3
3
module RSpec
4
4
module Rails
5
5
module Matchers
6
+ extend RSpec ::Matchers ::DSL
7
+
6
8
# Namespace for various implementations of ActionCable features
7
9
#
8
10
# @api private
@@ -50,7 +52,10 @@ def have_broadcasted_to(target = nil)
50
52
51
53
ActionCable ::HaveBroadcastedTo . new ( target , channel : described_class )
52
54
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
54
59
55
60
private
56
61
Original file line number Diff line number Diff line change @@ -51,6 +51,10 @@ def thrice
51
51
exactly ( :thrice )
52
52
end
53
53
54
+ def description
55
+ "have broadcasted #{ base_description } "
56
+ end
57
+
54
58
def failure_message
55
59
"expected to broadcast #{ base_message } " . tap do |msg |
56
60
if @unmatching_msgs . any?
@@ -140,18 +144,21 @@ def set_expected_number(relativity, count)
140
144
end
141
145
end
142
146
143
- def base_message
147
+ def base_description
144
148
"#{ message_expectation_modifier } #{ @expected_number } messages to #{ stream } " . tap do |msg |
145
149
msg << " with #{ data_description ( @data ) } " unless @data . nil?
146
- msg << ", but broadcast #{ @matching_msgs_count } "
147
150
end
148
151
end
149
152
153
+ def base_message
154
+ "#{ base_description } , but broadcast #{ @matching_msgs_count } "
155
+ end
156
+
150
157
def data_description ( data )
151
158
if data . is_a? ( RSpec ::Matchers ::Composable )
152
159
data . description
153
160
else
154
- data
161
+ data . inspect
155
162
end
156
163
end
157
164
Original file line number Diff line number Diff line change @@ -226,5 +226,33 @@ def broadcast(stream, msg)
226
226
end
227
227
end
228
228
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
229
257
end
230
258
end
You can’t perform that action at this time.
0 commit comments