Class: ActionCable::Connection::TestCase

Inherits:
ActiveSupport::TestCase
  • Object
show all
Includes:
Behavior
Defined in:
lib/action_cable/connection/test_case.rb

Overview

Superclass for Action Cable connection unit tests.

Basic example

Unit tests are written as follows:

  1. First, one uses the connect method to simulate connection.

  2. Then, one asserts whether the current state is as expected (e.g. identifiers).

For example:

module ApplicationCable
  class ConnectionTest < ActionCable::Connection::TestCase
    def test_connects_with_cookies
      # Simulate a connection
      connect cookies: { user_id: users[:john].id }

      # Asserts that the connection identifier is correct
      assert_equal "John", connection.user.name
  end

  def test_does_not_connect_without_user
    assert_reject_connection do
      connect
    end
  end
end

You can also provide additional information about underlying HTTP request:

def test_connect_with_headers_and_query_string
  connect "/cable?user_id=1", headers: { "X-API-TOKEN" => 'secret-my' }

  assert_equal connection.user_id, "1"
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

Method Summary

Methods included from Behavior

#connect, #disconnect

Methods included from Assertions

#assert_reject_connection