Skip to content

v0.3.0

Compare
Choose a tag to compare
@palkan palkan released this 02 Mar 13:56
· 66 commits to master since this release

Added support for connection unit-testing.

Minitest Usage

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

RSpec Usage

require "rails_helper"

RSpec.describe ApplicationCable::Connection, type: :channel do
  it "successfully connects" do
    connect "/cable", headers: { "X-USER-ID" => "325" }
    expect(connection.user_id).to eq "325"
  end

  it "rejects connection" do
    expect { connect "/cable" }.to have_rejected_connection
  end
end