Module: ActionCable::TestHelper

Included in:
Channel::TestCase::Behavior, TestCase
Defined in:
lib/action_cable/test_helper.rb

Overview

Provides helper methods for testing Action Cable broadcasting

Instance Method Summary collapse

Instance Method Details

#after_teardownObject

:nodoc:



16
17
18
19
# File 'lib/action_cable/test_helper.rb', line 16

def after_teardown # :nodoc:
  super
  ActionCable.server.instance_variable_set(:@pubsub, @old_pubsub_adapter)
end

#assert_broadcast_on(channel, data) ⇒ Object

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

def test_assert_transmited_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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/action_cable/test_helper.rb', line 96

def assert_broadcast_on(channel, data)
  # 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(channel)
  if block_given?
    old_messages = new_messages
    clear_messages(channel)

    yield
    new_messages = broadcasts(channel)
    clear_messages(channel)

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

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

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

#assert_broadcasts(channel, number) ⇒ Object

Asserts that the number of broadcasted messages to the channel 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


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/action_cable/test_helper.rb', line 45

def assert_broadcasts(channel, number)
  if block_given?
    original_count = broadcasts_size(channel)
    yield
    new_count = broadcasts_size(channel)
    assert_equal number, new_count - original_count, "#{number} broadcasts to #{channel} expected, but #{new_count - original_count} were sent"
  else
    actual_count = broadcasts_size(channel)
    assert_equal number, actual_count, "#{number} broadcasts to #{channel} expected, but #{actual_count} were sent"
  end
end

#assert_no_broadcasts(channel, &block) ⇒ Object

Asserts that no messages have been sent to the channel.

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


77
78
79
# File 'lib/action_cable/test_helper.rb', line 77

def assert_no_broadcasts(channel, &block)
  assert_broadcasts channel, 0, &block
end

#before_setupObject

:nodoc:



6
7
8
9
10
11
12
13
14
# File 'lib/action_cable/test_helper.rb', line 6

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

#pubsub_adapterObject

:nodoc:



120
121
122
# File 'lib/action_cable/test_helper.rb', line 120

def pubsub_adapter # :nodoc:
  ActionCable.server.pubsub
end