module ActionCable::TestHelper
def after_teardown # :nodoc:
def after_teardown # :nodoc: super ActionCable.server.instance_variable_set(:@pubsub, @old_pubsub_adapter) end
def assert_broadcast_on(stream, data, &block)
end
end
ActionCable.server.broadcast 'messages', text: 'hello'
assert_broadcast_on('messages', text: 'hello') do
def test_assert_broadcast_on_again
If a block is passed, that block should cause a message with the specified data to be sent.
end
assert_broadcast_on('messages', text: 'hello')
ActionCable.server.broadcast 'messages', text: 'hello'
def test_assert_transmitted_message
Asserts that the specified message has been sent to the stream.
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(&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
def assert_broadcasts(stream, number, &block)
end
end
ActionCable.server.broadcast 'messages', { text: 'how are you?' }
ActionCable.server.broadcast 'messages', { text: 'hi' }
assert_broadcasts('messages', 2) do
end
ActionCable.server.broadcast 'messages', { text: 'hello' }
assert_broadcasts('messages', 1) do
def test_broadcasts_again
messages to be broadcasted.
If a block is passed, that block should cause the specified number of
end
assert_broadcasts 'messages', 2
ActionCable.server.broadcast 'messages', { text: 'world' }
assert_broadcasts 'messages', 1
ActionCable.server.broadcast 'messages', { text: 'hello' }
assert_broadcasts 'messages', 0
def test_broadcasts
Asserts that the number of broadcasted messages to the stream matches the given number.
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
def assert_no_broadcasts(stream, &block)
assert_broadcasts 'messages', 0, &block
Note: This assertion is simply a shortcut for:
end
end
# No job messages should be sent from this block
assert_no_broadcasts 'messages' do
def test_broadcasts_again
If a block is passed, that block should not cause any message to be sent.
end
assert_broadcasts 'messages', 1
ActionCable.server.broadcast 'messages', { text: 'hi' }
assert_no_broadcasts 'messages'
def test_no_broadcasts
Asserts that no messages have been sent to the stream.
def assert_no_broadcasts(stream, &block) assert_broadcasts stream, 0, &block end
def before_setup # :nodoc:
def before_setup # :nodoc: server = ActionCable.server test_adapter = ActionCable::SubscriptionAdapter::Test.new(server) @old_pubsub_adapter = server.pubsub server.instance_variable_set(:@pubsub, test_adapter) super end
def broadcasts_size(channel)
def broadcasts_size(channel) broadcasts(channel).size end
def pubsub_adapter # :nodoc:
def pubsub_adapter # :nodoc: ActionCable.server.pubsub end