Module: ActionCable::Channel::TestCase::Behavior
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Instance Chain:
|
|
Defined in: | actioncable/lib/action_cable/channel/test_case.rb |
Constant Summary
-
CHANNEL_IDENTIFIER =
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 197"test_stub"
Class Method Summary
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
append_features, prepend_features |
Instance Attribute Summary
::ActionCable::TestHelper
- Included
Instance Method Summary
- #assert_broadcast_on(stream_or_object, *args)
-
#assert_broadcasts(stream_or_object, *args)
Enhance TestHelper assertions to handle non-String broadcastings.
-
#assert_has_no_stream(stream)
Asserts that the specified stream has not been started.
-
#assert_has_no_stream_for(object)
Asserts that the specified stream for a model has not started.
-
#assert_has_stream(stream)
Asserts that the specified stream has been started.
-
#assert_has_stream_for(object)
Asserts that the specified stream for a model has started.
-
#assert_no_streams
Asserts that no streams have been started.
-
#perform(action, data = {})
Perform action on a channel.
-
#stub_connection(identifiers = {})
Set up test connection with the specified identifiers:
-
#subscribe(params = {})
Subscribe to the channel under test.
-
#transmissions
Returns messages transmitted into channel.
-
#unsubscribe
Unsubscribe the subscription under test.
- #broadcasting_for(stream_or_object) private
- #check_subscribed! private
::ActionCable::TestHelper
- Included
#assert_broadcast_on | Asserts that the specified message has been sent to the stream. |
#assert_broadcasts | Asserts that the number of broadcasted messages to the stream matches the given number. |
#assert_no_broadcasts | Asserts that no messages have been sent to the stream. |
#capture_broadcasts | Returns the messages that are broadcasted in the block. |
#new_broadcasts_from, #after_teardown, #before_setup, #pubsub_adapter |
DSL Calls
included
[ GitHub ]199 200 201 202 203 204 205
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 199
included do class_attribute :_channel_class attr_reader :connection, :subscription ActiveSupport.run_load_hooks(:action_cable_channel_test_case, self) end
Instance Method Details
#assert_broadcast_on(stream_or_object, *args)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 282
def assert_broadcast_on(stream_or_object, *args) super(broadcasting_for(stream_or_object), *args) end
#assert_broadcasts(stream_or_object, *args)
Enhance TestHelper assertions to handle non-String broadcastings
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 278
def assert_broadcasts(stream_or_object, *args) super(broadcasting_for(stream_or_object), *args) end
#assert_has_no_stream(stream)
Asserts that the specified stream has not been started.
def test_assert_no_started_stream
subscribe
assert_has_no_stream 'messages'
end
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 326
def assert_has_no_stream(stream) assert subscription.streams.exclude?(stream), "Stream #{stream} has been started" end
#assert_has_no_stream_for(object)
Asserts that the specified stream for a model has not started.
def test_assert_no_started_stream_for
subscribe id: 41
assert_has_no_stream_for User.find(42)
end
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 337
def assert_has_no_stream_for(object) assert_has_no_stream(broadcasting_for(object)) end
#assert_has_stream(stream)
Asserts that the specified stream has been started.
def test_assert_started_stream
subscribe
assert_has_stream 'messages'
end
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 304
def assert_has_stream(stream) assert subscription.streams.include?(stream), "Stream #{stream} has not been started" end
#assert_has_stream_for(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
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 315
def assert_has_stream_for(object) assert_has_stream(broadcasting_for(object)) end
#assert_no_streams
Asserts that no streams have been started.
def test_assert_no_started_stream
subscribe
assert_no_streams
end
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 293
def assert_no_streams assert subscription.streams.empty?, "No streams started was expected, but #{subscription.streams.count} found" end
#broadcasting_for(stream_or_object) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 346
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! (private)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 342
def check_subscribed! raise "Must be subscribed!" if subscription.nil? || subscription.rejected? end
#perform(action, data = {})
Perform action on a channel.
NOTE: Must be subscribed.
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 266
def perform(action, data = {}) check_subscribed! subscription.perform_action(data.stringify_keys.merge("action" => action.to_s)) end
#stub_connection(identifiers = {})
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')
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 243
def stub_connection(identifiers = {}) @connection = ConnectionStub.new(identifiers) end
#subscribe(params = {})
Subscribe to the channel under test. Optionally pass subscription parameters as a ::Hash
.
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 249
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
#transmissions
Returns messages transmitted into channel
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 272
def transmissions # Return only directly sent message (via #transmit) connection.transmissions.filter_map { |data| data["message"] } end
#unsubscribe
Unsubscribe the subscription under test.
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 258
def unsubscribe check_subscribed! subscription.unsubscribe_from_channel end