Class: ActionCable::Connection::TestCase
- Inherits:
-
ActiveSupport::TestCase
- Object
- Minitest::Test
- ActiveSupport::TestCase
- ActionCable::Connection::TestCase
- Includes:
- Behavior
- Defined in:
- actioncable/lib/action_cable/connection/test_case.rb
Overview
Action Cable Connection TestCase
Unit test Action Cable connections.
Useful to check whether a connection’s identified_by
gets assigned properly and that any improper connection requests are rejected.
Basic example
Unit tests are written as follows:
-
Simulate a connection attempt by calling
connect
. -
Assert state, e.g. identifiers, has been assigned.
class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase
def
# Simulate the connection request with a cookie.
["user_id"] = users(:john).id
connect
# Assert the connection identifier matches the fixture.
assert_equal users(:john).id, connection.user.id
end
def
assert_reject_connection { connect }
end
end
connect
accepts additional information about the HTTP request with the params
, headers
, session
, and Rack env
options.
def test_connect_with_headers_and_query_string
connect params: { user_id: 1 }, headers: { "X-API-TOKEN" => "secret-my" }
assert_equal "1", connection.user.id
assert_equal "secret-my", connection.token
end
def test_connect_with_params
connect params: { user_id: 1 }
assert_equal "1", connection.user.id
end
You can also set up the correct cookies before the connection request:
def
# Plain cookies:
["user_id"] = 1
# Or signed/encrypted:
# cookies.signed["user_id"] = 1
# cookies.encrypted["user_id"] = 1
connect
assert_equal "1", connection.user_id
end
Connection is automatically inferred
ActionCable::Connection::TestCase will automatically infer the connection under test from the test class name. If the channel cannot be inferred from the test class name, you can explicitly set it with tests
.
class ConnectionTest < ActionCable::Connection::TestCase
tests ApplicationCable::Connection
end
Defined Under Namespace
Modules: Behavior
Constant Summary
Constants included from Behavior
Constants inherited from ActiveSupport::TestCase
ActiveSupport::TestCase::Assertion
Constants included from ActiveSupport::Testing::Assertions
ActiveSupport::Testing::Assertions::UNTRACKED
Method Summary
Methods included from Behavior
#connect, #cookies, #disconnect
Methods included from ActiveSupport::Concern
#append_features, #class_methods, extended, #included, #prepend_features, #prepended
Methods included from Assertions
Methods inherited from ActiveSupport::TestCase
#inspect, parallelize, parallelize_setup, parallelize_teardown, test_order, test_order=
Methods included from ActiveSupport::Testing::Declarative
Methods included from ActiveSupport::Testing::FileFixtures
Methods included from ActiveSupport::Testing::TimeHelpers
#after_teardown, #freeze_time, #travel, #travel_back, #travel_to
Methods included from ActiveSupport::Testing::ConstantStubbing
Methods included from ActiveSupport::Testing::Deprecation
#assert_deprecated, #assert_not_deprecated, #collect_deprecations
Methods included from ActiveSupport::Testing::Assertions
#assert_changes, #assert_difference, #assert_no_changes, #assert_no_difference, #assert_not, #assert_nothing_raised, #assert_raises