如何测试ActionCable连接?

时间:2018-11-07 23:46:21

标签: ruby rspec ruby-on-rails-5 actioncable

如何在RSpec中为ActionCable Connection编写测试?我已经发现documentationother questions有关测试Channel的信息,但是我很难找到有关测试Connection的信息。

1 个答案:

答案 0 :(得分:1)

action-cable-testing gem支持测试Connection。这里是文档:https://github.com/palkan/action-cable-testing#rspec-usage

这是文档中给出的RSpec示例:

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