Module: ActionCable::Channel::TestCase::Behavior
- Extended by:
- ActiveSupport::Concern
- Includes:
- TestHelper, ActiveSupport::Testing::ConstantLookup
- Defined in:
- lib/action_cable/testing/channel/test_case.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CHANNEL_IDENTIFIER =
"test_stub"
Constants included from TestHelper
Instance Method Summary collapse
- #assert_broadcast_on(stream_or_object, *args) ⇒ Object
-
#assert_broadcasts(stream_or_object, *args) ⇒ Object
Enhance TestHelper assertions to handle non-String broadcastings.
-
#assert_has_stream(stream) ⇒ Object
Asserts that the specified stream has been started.
-
#assert_has_stream_for(object) ⇒ Object
Asserts that the specified stream for a model has started.
-
#assert_no_streams ⇒ Object
Asserts that no streams have been started.
-
#perform(action, data = {}) ⇒ Object
Perform action on a channel.
-
#stub_connection(identifiers = {}) ⇒ Object
Setup test connection with the specified identifiers:.
-
#subscribe(params = {}) ⇒ Object
Subsribe to the channel under test.
-
#transmissions ⇒ Object
Returns messages transmitted into channel.
-
#unsubscribe ⇒ Object
Unsubscribe the subscription under test.
Methods included from TestHelper
#after_teardown, #assert_no_broadcasts, #before_setup, #pubsub_adapter
Instance Method Details
#assert_broadcast_on(stream_or_object, *args) ⇒ Object
250 251 252 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 250 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
246 247 248 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 246 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
272 273 274 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 272 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
283 284 285 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 283 def assert_has_stream_for(object) assert_has_stream(broadcasting_for(object)) end |
#assert_no_streams ⇒ Object
Asserts that no streams have been started.
def test_assert_no_started_stream
subscribe
assert_no_streams
end
261 262 263 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 261 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.
233 234 235 236 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 233 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')
207 208 209 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 207 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.
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 212 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 |
#transmissions ⇒ Object
Returns messages transmitted into channel
239 240 241 242 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 239 def transmissions # Return only directly sent message (via #transmit) connection.transmissions.map { |data| data["message"] }.compact end |
#unsubscribe ⇒ Object
Unsubscribe the subscription under test.
225 226 227 228 |
# File 'lib/action_cable/testing/channel/test_case.rb', line 225 def unsubscribe check_subscribed! subscription.unsubscribe_from_channel end |