gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:42 24 4
gpt4 key购买 nike

我有以下测试:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com