Module: RSpec::Rails::Matchers

Defined in:
lib/rspec/rails/matchers/action_cable.rb,
lib/rspec/rails/matchers/action_cable/have_streams.rb,
lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb

Defined Under Namespace

Modules: ActionCable

Instance Method Summary collapse

Instance Method Details

#have_broadcasted_to(target = nil) ⇒ Object Also known as: broadcast_to

Passes if a message has been sent to a stream/object inside a block. May chain ‘at_least`, `at_most` or `exactly` to specify a number of times. To specify channel from which message has been broadcasted to object use `from_channel`.

Examples:

expect {
  ActionCable.server.broadcast "messages", text: 'Hi!'
}.to have_broadcasted_to("messages")

expect {
  SomeChannel.broadcast_to(user)
}.to have_broadcasted_to(user).from_channel(SomeChannel)

# Using alias
expect {
  ActionCable.server.broadcast "messages", text: 'Hi!'
}.to broadcast_to("messages")

expect {
  ActionCable.server.broadcast "messages", text: 'Hi!'
  ActionCable.server.broadcast "all", text: 'Hi!'
}.to have_broadcasted_to("messages").exactly(:once)

expect {
  3.times { ActionCable.server.broadcast "messages", text: 'Hi!' }
}.to have_broadcasted_to("messages").at_least(2).times

expect {
  ActionCable.server.broadcast "messages", text: 'Hi!'
}.to have_broadcasted_to("messages").at_most(:twice)

expect {
  ActionCable.server.broadcast "messages", text: 'Hi!'
}.to have_broadcasted_to("messages").with(text: 'Hi!')


51
52
53
54
55
# File 'lib/rspec/rails/matchers/action_cable.rb', line 51

def have_broadcasted_to(target = nil)
  check_action_cable_adapter

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