Method: ActionCable::TestHelper#assert_broadcast_on
- Defined in:
- actioncable/lib/action_cable/test_helper.rb
#assert_broadcast_on(stream, data, &block) ⇒ Object
Asserts that the specified message has been sent to the stream.
def
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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'actioncable/lib/action_cable/test_helper.rb', line 116 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)) = broadcasts(stream) if block_given? = new_broadcasts_from(, stream, "assert_broadcast_on", &block) end = .find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg } = "No messages sent with #{data} to #{stream}" if .any? = .inject("#{}\nMessage(s) found:\n") do |, | + "#{ActiveSupport::JSON.decode()}\n" end else = "#{}\nNo message found for #{stream}" end assert , end |