module ActionCable::Channel::TestCase::Behavior

Included modules:
ActiveSupport::Testing::ConstantLookup, ActionCable::TestHelper, ActionCable::Channel::ChannelStub

Constants

CHANNEL_IDENTIFIER

Public Instance Methods

assert_broadcast_on(stream_or_object, *args) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 258
def assert_broadcast_on(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end
assert_broadcasts(stream_or_object, *args) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 254
def assert_broadcasts(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end

Enhance TestHelper assertions to handle non-String broadcastings

assert_has_stream(stream) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 280
def assert_has_stream(stream)
  assert subscription.streams.include?(stream), "Stream #{stream} has not been started"
end

Asserts that the specified stream has been started.

def test_assert_started_stream
  subscribe
  assert_has_stream 'messages'
end
assert_has_stream_for(object) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 291
def assert_has_stream_for(object)
  assert_has_stream(broadcasting_for(object))
end

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
assert_no_streams() Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 269
def assert_no_streams
  assert subscription.streams.empty?, "No streams started was expected, but #{subscription.streams.count} found"
end

Asserts that no streams have been started.

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

Perform action on a channel.

NOTE: Must be subscribed.

stub_connection(identifiers = {}) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 219
def stub_connection(identifiers = {})
  @connection = ConnectionStub.new(identifiers)
end

Setup 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')
subscribe(params = {}) Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 224
def subscribe(params = {})
  @connection ||= stub_connection
  @subscription = self.class.channel_class.new(connection, CHANNEL_IDENTIFIER, params.with_indifferent_access)
  @subscription.singleton_class.include(ChannelStub)
  @subscription.subscribe_to_channel
  @subscription
end

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

transmissions() Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 247
def transmissions
  # Return only directly sent message (via #transmit)
  connection.transmissions.map { |data| data["message"] }.compact
end

Returns messages transmitted into channel

unsubscribe() Show source
# File actioncable/lib/action_cable/channel/test_case.rb, line 233
def unsubscribe
  check_subscribed!
  subscription.unsubscribe_from_channel
end

Unsubscribe the subscription under test.

© 2004–2019 David Heinemeier Hansson
Licensed under the MIT License.