Class: ActionCable::Connection::TestCase
| Relationships & Source Files | |
| Namespace Children | |
| Modules: | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: 
          self,
           ::ActiveSupport::TestCase,::ActiveSupport::Testing::Declarative,
          Minitest::Test | |
| Instance Chain: | |
| Inherits: | ActiveSupport::TestCase 
 | 
| Defined in: | actioncable/lib/action_cable/connection/test_case.rb | 
Overview
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
endconnect 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
endYou 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
endConnection is automatically inferred
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
endConstant Summary
Behavior - Attributes & Methods
Class Attribute Summary
::ActiveSupport::TestCase - Inherited
| .file_fixture_path, .file_fixture_path?, | |
| .test_order | Returns the order in which test cases are run. | 
| .test_order= | Sets the order in which test cases are run. | 
Class Method Summary
::ActiveSupport::TestCase - Inherited
| .parallelize | Parallelizes the test suite. | 
| .parallelize_setup | Set up hook for parallel testing. | 
| .parallelize_teardown | Clean up hook for parallel testing. | 
::ActiveSupport::Testing::Declarative - Extended
Instance Attribute Summary
Instance Method Summary
Behavior - Included
| #connect | Performs connection attempt to exert  | 
| #cookies, | |
| #disconnect | Exert  | 
Assertions - Included
| #assert_reject_connection | Asserts that the connection is rejected (via  | 
::ActiveSupport::TestCase - Inherited
| #assert_no_match, #assert_not_empty, #assert_not_equal, #assert_not_in_delta, #assert_not_in_epsilon, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_nil, #assert_not_operator, #assert_not_predicate, #assert_not_respond_to, #assert_not_same, | |
| #assert_raise | test/unit backwards compatibility methods. | 
| #method_name | |
::ActiveSupport::Testing::FileFixtures - Included
| #file_fixture | Returns a  | 
::ActiveSupport::Testing::TimeHelpers - Included
| #after_teardown, | |
| #freeze_time | Calls  | 
| #travel | Changes current time to the time in the future or in the past by a given time difference by stubbing  | 
| #travel_back | Returns the current time back to its original state, by removing the stubs added by  | 
| #travel_to | Changes current time to the given time by stubbing  | 
| #unfreeze_time | |
::ActiveSupport::Testing::Assertions - Included
| #assert_changes | Assertion that the result of evaluating an expression is changed before and after invoking the passed in block. | 
| #assert_difference | Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block. | 
| #assert_no_changes | Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block. | 
| #assert_no_difference | Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block. | 
| #assert_not | Asserts that an expression is not truthy. | 
| #assert_nothing_raised | Assertion that the block should not raise an exception. | 
Class Attribute Details
._connection_class (rw)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/test_case.rb', line 139
class_attribute :_connection_class
    ._connection_class?  ⇒ Boolean  (rw)
  
  [ GitHub ]
# File 'actioncable/lib/action_cable/connection/test_case.rb', line 139
class_attribute :_connection_class
Instance Attribute Details
#_connection_class (rw)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/test_case.rb', line 139
class_attribute :_connection_class
    #_connection_class?  ⇒ Boolean  (rw)
  
  [ GitHub ]
# File 'actioncable/lib/action_cable/connection/test_case.rb', line 139
class_attribute :_connection_class
#connection (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/test_case.rb', line 141
attr_reader :connection