我有以下测试:
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 requiringenvironment.rb
:
require "action_cable/testing/rspec"
关于ruby - ActionCable channel 的 RSpec 测试中未定义方法 `stub_connection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213201/