【问题标题】:How do I test an ActionCable connection?如何测试 ActionCable 连接?
【发布时间】:2019-04-11 11:33:18
【问题描述】:

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

【问题讨论】:

  • 我找到了this question,它链接到this GitHub issue comment,显示了一种潜在的技术。但是,我不确定这是否是我想在自己的代码中使用的理智模式。
  • 什么是“不理智”?
  • @lacostenycoder 测试设置为type: :channel。我很紧张使用一些 RSpec/Rails 魔法来使 Channel 测试更容易编写,但对使用 Connection 没有帮助。
  • 我想没有看到你到底在测试什么,我无法对此发表进一步评论。

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


【解决方案1】:

action-cable-testing gem 包括对测试Connections 的支持。这是文档: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

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2018-07-07
    • 1970-01-01
    • 2016-07-06
    • 2018-11-06
    • 2016-05-15
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多