ruby - ActionCable channel 的 RSpec 测试中未定义方法 `stub_connection'

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

我有以下测试:

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 channel 测试中自动可用。我做错了什么?

最佳答案

我需要安装 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 Usage

First, you need to have rspec-rails installed.

Second, add this to your "rails_helper.rb" after requiring environment.rb:

require "action_cable/testing/rspec"

关于ruby - ActionCable channel 的 RSpec 测试中未定义方法 `stub_connection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213201/

相关文章:

ruby-on-rails - 为什么 Rails 无法启动并显示 "Expected to find a manifest file in ` app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)”?

ruby-on-rails - 出现错误 : NoMethodError: undefined method `fetch_value' for nil:NilClass when trying to create a new record in the rails console

ruby - 为什么这个Hash of arrays的size是0?

python - 正则表达式在 Python 和 Ruby 中的工作方式不同

ruby - ruby、rspec 中的describe、it 和def 之间的区别

ruby-on-rails - 通过数据属性查找并填写输入字段

ruby-on-rails - rails 中 mock 和 stubs 的实际例子

ruby-on-rails - 无法将数据从 collection_select 助手保存到模型

ruby - Ruby 方法的双冒号(双列或::)语法的惯用用法

ruby - 无法从远程计算机运行 sh 脚本