Module: ActionCable::Channel::TestCase::Behavior
- Extended by:
- ActiveSupport::Concern
- Includes:
- TestHelper, ActiveSupport::Testing::ConstantLookup
- Included in:
- ActionCable::Channel::TestCase
- Defined in:
- lib/action_cable/channel/test_case.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CHANNEL_IDENTIFIER =
"test_stub"
Instance Attribute Summary collapse
-
#subscription ⇒ Object
readonly
Use testserver (not test_server) to silence “Test is missing assertions: ‘test_server`” warnings.
-
#testserver ⇒ Object
readonly
Use testserver (not test_server) to silence “Test is missing assertions: ‘test_server`” warnings.
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_no_stream(stream) ⇒ Object
Asserts that the specified stream has not been started.
-
#assert_has_no_stream_for(object) ⇒ Object
Asserts that the specified stream for a model has not started.
-
#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(server: ActionCable.server, **identifiers) ⇒ Object
Set up test connection with the specified identifiers:.
-
#subscribe(params = {}) ⇒ Object
Subscribe 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, #capture_broadcasts, #pubsub_adapter
Instance Attribute Details
#subscription ⇒ Object (readonly)
Use testserver (not test_server) to silence “Test is missing assertions: ‘test_server`” warnings
198 199 200 |
# File 'lib/action_cable/channel/test_case.rb', line 198 def subscription @subscription end |
#testserver ⇒ Object (readonly)
Use testserver (not test_server) to silence “Test is missing assertions: ‘test_server`” warnings
198 199 200 |
# File 'lib/action_cable/channel/test_case.rb', line 198 def testserver @testserver end |
Instance Method Details
#assert_broadcast_on(stream_or_object, *args) ⇒ Object
252 253 254 |
# File 'lib/action_cable/channel/test_case.rb', line 252 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
248 249 250 |
# File 'lib/action_cable/channel/test_case.rb', line 248 def assert_broadcasts(stream_or_object, *args) super(broadcasting_for(stream_or_object), *args) end |
#assert_has_no_stream(stream) ⇒ Object
Asserts that the specified stream has not been started.
def test_assert_no_started_stream
subscribe
assert_has_no_stream 'messages'
end
298 299 300 301 |
# File 'lib/action_cable/channel/test_case.rb', line 298 def assert_has_no_stream(stream) check_subscribed! assert subscription.stream_names.exclude?(stream), "Stream #{stream} has been started" end |
#assert_has_no_stream_for(object) ⇒ 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
310 311 312 |
# File 'lib/action_cable/channel/test_case.rb', line 310 def assert_has_no_stream_for(object) assert_has_no_stream(broadcasting_for(object)) 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
275 276 277 278 |
# File 'lib/action_cable/channel/test_case.rb', line 275 def assert_has_stream(stream) check_subscribed! assert subscription.stream_names.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
287 288 289 |
# File 'lib/action_cable/channel/test_case.rb', line 287 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
263 264 265 266 |
# File 'lib/action_cable/channel/test_case.rb', line 263 def assert_no_streams check_subscribed! assert subscription.stream_names.empty?, "No streams started was expected, but #{subscription.stream_names.count} found" end |
#perform(action, data = {}) ⇒ Object
Perform action on a channel.
NOTE: Must be subscribed.
236 237 238 239 |
# File 'lib/action_cable/channel/test_case.rb', line 236 def perform(action, data = {}) check_subscribed! subscription.perform_action(data.stringify_keys.merge("action" => action.to_s)) end |
#stub_connection(server: ActionCable.server, **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')
207 208 209 210 211 212 213 214 215 |
# File 'lib/action_cable/channel/test_case.rb', line 207 def stub_connection(server: ActionCable.server, **identifiers) @socket = Connection::TestSocket.new(Connection::TestSocket.build_request(ActionCable.server.config.mount_path || "/cable")) @testserver = Connection::TestServer.new(server) @connection = self.class.connection_class.new(testserver, socket).tap do |conn| identifiers.each do |identifier, val| conn.public_send("#{identifier}=", val) end end end |
#subscribe(params = {}) ⇒ Object
Subscribe to the channel under test. Optionally pass subscription parameters as a Hash.
219 220 221 222 223 224 225 |
# File 'lib/action_cable/channel/test_case.rb', line 219 def subscribe(params = {}) @connection ||= stub_connection @subscription = self.class.channel_class.new(connection, CHANNEL_IDENTIFIER, params.with_indifferent_access) @subscription.singleton_class.include(ChannelExt) @subscription.subscribe_to_channel @subscription end |
#transmissions ⇒ Object
Returns messages transmitted into channel
242 243 244 245 |
# File 'lib/action_cable/channel/test_case.rb', line 242 def transmissions # Return only directly sent message (via #transmit) socket.transmissions.filter_map { |data| data["message"] } end |
#unsubscribe ⇒ Object
Unsubscribe the subscription under test.
228 229 230 231 |
# File 'lib/action_cable/channel/test_case.rb', line 228 def unsubscribe check_subscribed! subscription.unsubscribe_from_channel end |