RSpec 测试 ActionCable 通道的未定义方法“stub_connection”
Undefined method `stub_connection' in RSpec test for ActionCable channel
我有以下测试:
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 频道测试中自动可用。我做错了什么?
我需要安装 action-cable-testing gem. As of when I'm writing this answer, this gem's functionality is slated to be integrated directly into Rails at some point in Rails 6。
installing the gem, make sure to 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"
我有以下测试:
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 频道测试中自动可用。我做错了什么?
我需要安装 action-cable-testing gem. As of when I'm writing this answer, this gem's functionality is slated to be integrated directly into Rails at some point in Rails 6。
installing the gem, make sure to 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"