How do I test an ActionCable connection?

How do I write a test for an ActionCable Connection in RSpec? I've found documentation and other questions about testing Channels, but I'm having a hard time finding information about testing Connections.

1 answer

  • answered 2018-11-08 20:50 Kevin

    The action-cable-testing gem includes support for testing Connections. Here's the documentation: https://github.com/palkan/action-cable-testing#rspec-usage

    And here's the RSpec example given in the documentation:

    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