instance method assert_broadcast_on

Ruby on Rails 7.0.10

Since v6.0.6

Available in: v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

assert_broadcast_on(stream, data, &block)

Asserts that the specified message has been sent to the stream.

def test_assert_transmitted_message
  ActionCable.server.broadcast 'messages', text: 'hello'
  assert_broadcast_on('messages', text: 'hello')
end

If a block is passed, that block should cause a message with the specified data to be sent.

def test_assert_broadcast_on_again
  assert_broadcast_on('messages', text: 'hello') do
    ActionCable.server.broadcast 'messages', text: 'hello'
  end
end

Parameters

stream req
data req
block block
Source
# File actioncable/lib/action_cable/test_helper.rb, line 97
    def assert_broadcast_on(stream, data, &block)
      # Encode to JSON and back–we want to use this value to compare
      # with decoded JSON.
      # Comparing JSON strings doesn't work due to the order if the keys.
      serialized_msg =
        ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(data))

      new_messages = broadcasts(stream)
      if block_given?
        old_messages = new_messages
        clear_messages(stream)

        _assert_nothing_raised_or_warn("assert_broadcast_on", &block)
        new_messages = broadcasts(stream)
        clear_messages(stream)

        # Restore all sent messages
        (old_messages + new_messages).each { |m| pubsub_adapter.broadcast(stream, m) }
      end

      message = new_messages.find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg }

      assert message, "No messages sent with #{data} to #{stream}"
    end

Defined in actioncable/lib/action_cable/test_helper.rb line 97 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionCable::TestHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close