Module: ActionCable::Channel::TestCase::Behavior

Extended by:
ActiveSupport::Concern
Includes:
TestHelper, ActiveSupport::Testing::ConstantLookup
Included in:
ActionCable::Channel::TestCase, RSpec::Rails::ChannelExampleGroup
Defined in:
lib/action_cable/channel/test_case.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CHANNEL_IDENTIFIER =
"test_stub"

Instance Method Summary collapse

Methods included from TestHelper

#after_teardown, #assert_broadcast_on, #assert_broadcasts, #assert_no_broadcasts, #before_setup, #pubsub_adapter

Instance Method Details

#connectionObject

:nodoc:



208
209
210
# File 'lib/action_cable/channel/test_case.rb', line 208

def connection # :nodoc:
  @connection ||= stub_connection
end

#perform(action, data = {}) ⇒ Object

Perform action on a channel.

NOTE: Must be subscribed.



203
204
205
206
# File 'lib/action_cable/channel/test_case.rb', line 203

def perform(action, data = {})
  check_subscribed!
  subscription.perform_action(data.stringify_keys.merge("action" => action.to_s))
end

#stub_connection(identifiers = {}) ⇒ Object

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')


188
189
190
# File 'lib/action_cable/channel/test_case.rb', line 188

def stub_connection(identifiers = {})
  @connection = ConnectionStub.new(identifiers)
end

#subscribe(params = {}) ⇒ Object

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



193
194
195
196
197
198
# File 'lib/action_cable/channel/test_case.rb', line 193

def subscribe(params = {})
  @subscription = self.class.channel_class.new(connection, CHANNEL_IDENTIFIER, params.with_indifferent_access)
  @subscription.singleton_class.include(ChannelStub)
  @subscription.subscribe_to_channel
  @subscription
end

#transmissionsObject

Returns messages transmitted into channel



213
214
215
216
# File 'lib/action_cable/channel/test_case.rb', line 213

def transmissions
  # Return only directly sent message (via #transmit)
  connection.transmissions.map { |data| data["message"] }.compact
end