module ActionCable::Channel::TestCase::Behavior

Constants

CHANNEL_IDENTIFIER

Public Instance Methods

assert_broadcast_on(stream_or_object, *args) click to toggle source
# File lib/action_cable/testing/channel/test_case.rb, line 253
def assert_broadcast_on(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end
assert_broadcasts(stream_or_object, *args) click to toggle source

Enhance TestHelper assertions to handle non-String broadcastings

# File lib/action_cable/testing/channel/test_case.rb, line 249
def assert_broadcasts(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end
assert_has_stream(stream) click to toggle source

Asserts that the specified stream has been started.

def test_assert_started_stream
  subscribe
  assert_has_stream 'messages'
end
# File lib/action_cable/testing/channel/test_case.rb, line 275
def assert_has_stream(stream)
  assert subscription.streams.include?(stream), "Stream #{stream} has not been started"
end
assert_has_stream_for(object) click to toggle source

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 lib/action_cable/testing/channel/test_case.rb, line 286
def assert_has_stream_for(object)
  assert_has_stream(broadcasting_for(object))
end
assert_no_streams() click to toggle source

Asserts that no streams have been started.

def test_assert_no_started_stream
  subscribe
  assert_no_streams
end
# File lib/action_cable/testing/channel/test_case.rb, line 264
def assert_no_streams
  assert subscription.streams.empty?, "No streams started was expected, but #{subscription.streams.count} found"
end
perform(action, data = {}) click to toggle source

Perform action on a channel.

NOTE: Must be subscribed.

# File lib/action_cable/testing/channel/test_case.rb, line 236
def perform(action, data = {})
  check_subscribed!
  subscription.perform_action(data.stringify_keys.merge("action" => action.to_s))
end
streams() click to toggle source
# File lib/action_cable/testing/channel/test_case.rb, line 164
def streams
  ActiveSupport::Deprecation.warn "Use appropriate `assert_has_stream`, `assert_has_stream_for`, `assert_no_streams` " +
    "assertion methods for minitest or `have_stream`, `have_stream_for` and `have_stream_from` matchers " +
    "for RSpec. Direct access to `streams` is deprecated and is going to be removed in version 1.0"
  subscription.streams
end
stub_connection(identifiers = {}) click to toggle source

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')
# File lib/action_cable/testing/channel/test_case.rb, line 210
def stub_connection(identifiers = {})
  @connection = ConnectionStub.new(identifiers)
end
subscribe(params = {}) click to toggle source

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

# File lib/action_cable/testing/channel/test_case.rb, line 215
def subscribe(params = {})
  @connection ||= stub_connection
  # NOTE: Rails < 5.0.1 calls subscribe_to_channel during #initialize.
  #       We have to stub before it
  @subscription = self.class.channel_class.allocate
  @subscription.singleton_class.include(ChannelStub)
  @subscription.send(:initialize, connection, CHANNEL_IDENTIFIER, params.with_indifferent_access)
  # Call subscribe_to_channel if it's public (Rails 5.0.1+)
  @subscription.subscribe_to_channel if ActionCable.gem_version >= Gem::Version.new("5.0.1")
  @subscription
end
transmissions() click to toggle source

Returns messages transmitted into channel

# File lib/action_cable/testing/channel/test_case.rb, line 242
def transmissions
  # Return only directly sent message (via #transmit)
  connection.transmissions.map { |data| data["message"] }.compact
end
unsubscribe() click to toggle source

Unsubscribe the subscription under test.

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

Private Instance Methods

broadcasting_for(stream_or_object) click to toggle source
# File lib/action_cable/testing/channel/test_case.rb, line 295
def broadcasting_for(stream_or_object)
  return stream_or_object if stream_or_object.is_a?(String)

  self.class.channel_class.broadcasting_for(stream_or_object)
end
check_subscribed!() click to toggle source
# File lib/action_cable/testing/channel/test_case.rb, line 291
def check_subscribed!
  raise "Must be subscribed!" if subscription.nil? || subscription.rejected?
end