diff --git a/lib/rspec/rails/matchers/action_cable.rb b/lib/rspec/rails/matchers/action_cable.rb index 12b811d7b..096febc3c 100644 --- a/lib/rspec/rails/matchers/action_cable.rb +++ b/lib/rspec/rails/matchers/action_cable.rb @@ -45,12 +45,17 @@ module ActionCable # expect { # ActionCable.server.broadcast "messages", text: 'Hi!' # }.to have_broadcasted_to("messages").with(text: 'Hi!') + extend RSpec::Matchers::DSL + def have_broadcasted_to(target = nil) check_action_cable_adapter 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 diff --git a/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb b/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb index 297d7cfcf..45bdc5ece 100644 --- a/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +++ b/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb @@ -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? @@ -140,9 +144,14 @@ 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? + end + end + + def base_message + base_description.tap do |msg| msg << ", but broadcast #{@matching_msgs_count}" end end @@ -151,7 +160,7 @@ def data_description(data) if data.is_a?(RSpec::Matchers::Composable) data.description else - data + data.inspect end end diff --git a/spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb b/spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb index 815a9225a..c63ea9396 100644 --- a/spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb +++ b/spec/rspec/rails/matchers/action_cable/have_broadcasted_to_spec.rb @@ -226,5 +226,34 @@ def broadcast(stream, msg) end end end + + it "has an appropriate description when not qualified with a chain method" 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 not qualified with a chain method and 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 + + 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