首页 > 解决方案 > 如何测试 ActionCable 连接?

问题描述

如何Connection在 RSpec 中为 ActionCable 编写测试?我找到了有关 testing的文档其他 问题Channel,但是我很难找到有关 testing的信息Connection

标签: rubyrspecruby-on-rails-5actioncable

解决方案


action-cable-testing gem包括对 testing的支持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

推荐阅读