Action Cable Testing Versions Save

Action Cable testing utils

v0.6.0

4 years ago
  • Fixed Rails 6 compatibility.
  • Dropped Ruby 2.3 support.

v0.5.0

5 years ago

The gem has been merged into Rails and is already available in Rails 6 Beta 1.

This release makes the gem API compatible with the Rails 6. Incompatible API has been deprecated (though still available and produce warnings) and will be removed in v1.0.

See the official testing guide: https://edgeguides.rubyonrails.org/testing.html#testing-action-cable

NOTE: You can still use the gem with Rails 6 if you need RSpec functionality (it's planned to be merged into RSpec 4).

v0.3.0

6 years ago

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