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

Extended by:
ActiveSupport::Concern
Includes:
ActionCable::Connection::TestCase::Behavior, 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"

Constants included from TestHelper

TestHelper::CHANNEL_NOT_FOUND

Instance Method Summary collapse

Methods included from ActionCable::Connection::TestCase::Behavior

#connect, #disconnect

Methods included from ActionCable::Connection::Assertions

#assert_reject_connection

Methods included from TestHelper

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

Instance Method Details

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

Perform action on a channel.

NOTE: Must be subscribed.



219
220
221
222
# File 'lib/action_cable/channel/test_case.rb', line 219

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


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

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.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/action_cable/channel/test_case.rb', line 198

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

#transmissionsObject

Returns messages transmitted into channel



225
226
227
228
# File 'lib/action_cable/channel/test_case.rb', line 225

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

#unsubscribeObject

Unsubscribe the subscription under test.



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

def unsubscribe
  check_subscribed!
  subscription.unsubscribe_from_channel
end