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:



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

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

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

Perform action on a channel.

NOTE: Must be subscribed.



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

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
199
200
201
202
# File 'lib/action_cable/channel/test_case.rb', line 193

def subscribe(params = {})
  # 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

#transmissionsObject

Returns messages transmitted into channel



217
218
219
220
# File 'lib/action_cable/channel/test_case.rb', line 217

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