Module: ActionCable::TestHelper
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | actioncable/lib/action_cable/test_helper.rb |
Overview
Provides helper methods for testing Action Cable broadcasting
Instance Attribute Summary
- #broadcasts readonly
- #clear_messages readonly
Instance Method Summary
-
#assert_broadcast_on(stream, data, &block)
Asserts that the specified message has been sent to the stream.
-
#assert_broadcasts(stream, number, &block)
Asserts that the number of broadcasted messages to the stream matches the given number.
-
#assert_no_broadcasts(stream, &block)
Asserts that no messages have been sent to the stream.
Instance Attribute Details
#broadcasts (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/test_helper.rb', line 126
delegate :broadcasts, :, to: :pubsub_adapter
#clear_messages (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/test_helper.rb', line 126
delegate :broadcasts, :, to: :pubsub_adapter
Instance Method Details
#assert_broadcast_on(stream, data, &block)
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
# 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)) = broadcasts(stream) if block_given? = (stream) assert_nothing_raised(&block) = broadcasts(stream) (stream) # Restore all sent messages ( + ).each { |m| pubsub_adapter.broadcast(stream, m) } end = .find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg } assert , "No messages sent with #{data} to #{stream}" end
#assert_broadcasts(stream, number, &block)
Asserts that the number of broadcasted messages to the stream matches the given number.
def test_broadcasts
assert_broadcasts 'messages', 0
ActionCable.server.broadcast 'messages', { text: 'hello' }
assert_broadcasts 'messages', 1
ActionCable.server.broadcast 'messages', { text: 'world' }
assert_broadcasts 'messages', 2
end
If a block is passed, that block should cause the specified number of messages to be broadcasted.
def test_broadcasts_again
assert_broadcasts('messages', 1) do
ActionCable.server.broadcast 'messages', { text: 'hello' }
end
assert_broadcasts('messages', 2) do
ActionCable.server.broadcast 'messages', { text: 'hi' }
ActionCable.server.broadcast 'messages', { text: 'how are you?' }
end
end
# File 'actioncable/lib/action_cable/test_helper.rb', line 45
def assert_broadcasts(stream, number, &block) if block_given? original_count = broadcasts_size(stream) assert_nothing_raised(&block) new_count = broadcasts_size(stream) actual_count = new_count - original_count else actual_count = broadcasts_size(stream) end assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent" end
#assert_no_broadcasts(stream, &block)
Asserts that no messages have been sent to the stream.
def test_no_broadcasts
assert_no_broadcasts 'messages'
ActionCable.server.broadcast 'messages', { text: 'hi' }
assert_broadcasts 'messages', 1
end
If a block is passed, that block should not cause any message to be sent.
def test_broadcasts_again
assert_no_broadcasts 'messages' do
# No job messages should be sent from this block
end
end
Note: This assertion is simply a shortcut for:
assert_broadcasts 'messages', 0, &block
# File 'actioncable/lib/action_cable/test_helper.rb', line 78
def assert_no_broadcasts(stream, &block) assert_broadcasts stream, 0, &block end