Skip to Content Skip to Search
Namespace
Methods
A
P
S
T
U
Included Modules

Constants

CHANNEL_IDENTIFIER = "test_stub"
 

Attributes

[R] subscription

Use testserver (not test_server) to silence “Test is missing assertions: test_server” warnings

[R] testserver

Use testserver (not test_server) to silence “Test is missing assertions: test_server” warnings

Instance Public methods

advance_time(seconds)

Advances all registered periodic timers by the given number of seconds, firing their blocks for each elapsed interval:

def test_timer subscribe advance_time 5.seconds assert_equal 1, subscription.tick_count end

# File actioncable/lib/action_cable/channel/test_case.rb, line 235
def advance_time(seconds)
  testserver.advance_time(seconds)
end

assert_broadcast_on(stream_or_object, *args)

# File actioncable/lib/action_cable/channel/test_case.rb, line 264
def assert_broadcast_on(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end

assert_broadcasts(stream_or_object, *args)

Enhance TestHelper assertions to handle non-String broadcastings

# File actioncable/lib/action_cable/channel/test_case.rb, line 260
def assert_broadcasts(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end

assert_has_no_stream(stream)

Asserts that the specified stream has not been started.

def test_assert_no_started_stream
  subscribe
  assert_has_no_stream 'messages'
end
# File actioncable/lib/action_cable/channel/test_case.rb, line 310
def assert_has_no_stream(stream)
  check_subscribed!
  assert subscription.stream_names.exclude?(stream), "Stream #{stream} has been started"
end

assert_has_no_stream_for(object)

Asserts that the specified stream for a model has not started.

def test_assert_no_started_stream_for
  subscribe id: 41
  assert_has_no_stream_for User.find(42)
end
# File actioncable/lib/action_cable/channel/test_case.rb, line 322
def assert_has_no_stream_for(object)
  assert_has_no_stream(broadcasting_for(object))
end

assert_has_stream(stream)

Asserts that the specified stream has been started.

def test_assert_started_stream
  subscribe
  assert_has_stream 'messages'
end
# File actioncable/lib/action_cable/channel/test_case.rb, line 287
def assert_has_stream(stream)
  check_subscribed!
  assert subscription.stream_names.include?(stream), "Stream #{stream} has not been started"
end

assert_has_stream_for(object)

Asserts that the specified stream for a model has started.

def test_assert_started_stream_for
  subscribe id: 42
  assert_has_stream_for User.find(42)
end
# File actioncable/lib/action_cable/channel/test_case.rb, line 299
def assert_has_stream_for(object)
  assert_has_stream(broadcasting_for(object))
end

assert_no_streams()

Asserts that no streams have been started.

def test_assert_no_started_stream
  subscribe
  assert_no_streams
end
# File actioncable/lib/action_cable/channel/test_case.rb, line 275
def assert_no_streams
  check_subscribed!
  assert subscription.stream_names.empty?, "No streams started was expected, but #{subscription.stream_names.count} found"
end

perform(action, data = {})

Perform action on a channel.

NOTE: Must be subscribed.

# File actioncable/lib/action_cable/channel/test_case.rb, line 248
def perform(action, data = {})
  check_subscribed!
  subscription.perform_action(data.stringify_keys.merge("action" => action.to_s))
end

stub_connection(server: ActionCable.server, **identifiers)

Set up test connection with the specified identifiers:

class ApplicationCable < ActionCable::Connection::Base
  identified_by :user, :token
end

stub_connection(user: users[:john], token: 'my-secret-token')
# File actioncable/lib/action_cable/channel/test_case.rb, line 207
def stub_connection(server: ActionCable.server, **identifiers)
  @socket = Connection::TestSocket.new(Connection::TestSocket.build_request(ActionCable.server.config.mount_path || "/cable"))
  @testserver = Connection::TestServer.new(server)
  @connection = self.class.connection_class.new(testserver, socket).tap do |conn|
    identifiers.each do |identifier, val|
      conn.public_send("#{identifier}=", val)
    end
  end
end

subscribe(params = {})

Subscribe to the channel under test. Optionally pass subscription parameters as a Hash.

# File actioncable/lib/action_cable/channel/test_case.rb, line 219
def subscribe(params = {})
  @connection ||= stub_connection
  @subscription = self.class.channel_class.new(connection, CHANNEL_IDENTIFIER, params.with_indifferent_access)
  @subscription.singleton_class.include(ChannelExt)
  @subscription.subscribe_to_channel
  @subscription
end

transmissions()

Returns messages transmitted into channel

# File actioncable/lib/action_cable/channel/test_case.rb, line 254
def transmissions
  # Return only directly sent message (via #transmit)
  socket.transmissions.filter_map { |data| data["message"] }
end

unsubscribe()

Unsubscribe the subscription under test.

# File actioncable/lib/action_cable/channel/test_case.rb, line 240
def unsubscribe
  check_subscribed!
  subscription.unsubscribe_from_channel
end