1

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.

Kevin
  • 14,655
  • 24
  • 74
  • 124
  • I found [this question](https://stackoverflow.com/questions/50549428/how-to-test-actioncable-along-with-devise-using-rspec), which links to [this GitHub issue comment](https://github.com/palkan/action-cable-testing/issues/26#issuecomment-392481619) showing a potential technique. However, I'm not sure whether that's a sane pattern I want to use in my own code. – Kevin Nov 07 '18 at 23:47
  • What is "not sane" about it? – lacostenycoder Nov 08 '18 at 00:07
  • @lacostenycoder The test is set up as `type: :channel`. I'm nervous that's using some RSpec/Rails magic meant to make `Channel` tests more easier to write but isn't helpful specifically for working with a `Connection`. – Kevin Nov 08 '18 at 04:21
  • I guess without seeing what exactly it is you're testing for, I can't comment further on this. – lacostenycoder Nov 08 '18 at 06:32

1 Answers1

2

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
Kevin
  • 14,655
  • 24
  • 74
  • 124