【问题标题】:Undefined method `stub_connection' in RSpec test for ActionCable channelActionCable 通道的 RSpec 测试中未定义的方法“stub_connection”
【发布时间】:2019-04-12 06:48:53
【问题描述】:

我有以下测试:

require "rails_helper"

RSpec.describe MyChannel, type: :channel do
  let (:current_user) { User.first }

  it "subscribes to a unique room for the user" do
    stub_connection current_user: User.first
    subscribe
    expect(subscription).to be_confirmed
    expect(streams).to include("my_channel_#{current_user.id}")
  end
end

失败并出现此错误:

 Failure/Error: stub_connection current_user: User.first

 NoMethodError:
   undefined method `stub_connection' for #<RSpec::ExampleGroups::MyChannel:0x00000000deadbeef>
 # ./spec/channels/my_channel_spec.rb:7:in `block (2 levels) in <top (required)>'

根据the documentation 和我发现的所有示例,stub_connection 应该在 RSpec 通道测试中自动可用。我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    我需要安装action-cable-testing gem。在我写这个答案时,这个 gem 的功能是 slated to be integrated directly into Rails at some point in Rails 6

    installing the gem之后,一定要require the gem in your rails_helper.rb file

    RSpec 用法

    首先,您需要安装 rspec-rails。

    其次,在要求 environment.rb 之后将其添加到您的 "rails_helper.rb"

    require "action_cable/testing/rspec"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多