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

Extended by:
ActiveSupport::Concern
Includes:
TestHelper, ActiveSupport::Testing::ConstantLookup
Included in:
ActionCable::Channel::TestCase
Defined in:
actioncable/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 ActiveSupport::Concern

append_features, class_methods, extended, included, prepend_features, prepended

Methods included from TestHelper

#after_teardown, #assert_no_broadcasts, #before_setup, #pubsub_adapter

Instance Method Details

#assert_broadcast_on(stream_or_object, *args) ⇒ Object



274
275
276
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 274

def assert_broadcast_on(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end

#assert_broadcasts(stream_or_object, *args) ⇒ Object

Enhance TestHelper assertions to handle non-String broadcastings



270
271
272
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 270

def assert_broadcasts(stream_or_object, *args)
  super(broadcasting_for(stream_or_object), *args)
end

#assert_has_stream(stream) ⇒ Object

Asserts that the specified stream has been started.

def test_assert_started_stream
  subscribe
  assert_has_stream 'messages'
end


296
297
298
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 296

def assert_has_stream(stream)
  assert subscription.streams.include?(stream), "Stream #{stream} has not been started"
end

#assert_has_stream_for(object) ⇒ Object

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


307
308
309
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 307

def assert_has_stream_for(object)
  assert_has_stream(broadcasting_for(object))
end

#assert_no_streamsObject

Asserts that no streams have been started.

def test_assert_no_started_stream
  subscribe
  assert_no_streams
end


285
286
287
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 285

def assert_no_streams
  assert subscription.streams.empty?, "No streams started was expected, but #{subscription.streams.count} found"
end

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

Perform action on a channel.

NOTE: Must be subscribed.



257
258
259
260
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 257

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

#stub_connection(identifiers = {}) ⇒ Object

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


235
236
237
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 235

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

#subscribe(params = {}) ⇒ Object

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



240
241
242
243
244
245
246
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 240

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

#transmissionsObject

Returns messages transmitted into channel



263
264
265
266
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 263

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

#unsubscribeObject

Unsubscribe the subscription under test.



249
250
251
252
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 249

def unsubscribe
  check_subscribed!
  subscription.unsubscribe_from_channel
end