如何使用 RSpec 测试 ActionCable 频道? [英] How can I test ActionCable channels using RSpec?
问题描述
我想知道如何测试 ActionCable 频道.
I am wondering how to test ActionCable channels.
假设我有以下聊天频道:
Let's say I have the following chat channel:
class ChatChannel < ApplicationCable::Channel
def subscribed
current_user.increment!(:num_of_chats)
stream_from "chat_#{params[:chat_id]}"
stream_from "chat_stats_#{params[:chat_id]}"
end
end
subscribed
方法更新数据库并定义两个跨频道广播的流,但细节不是很重要,因为我的问题是一个更一般的问题:
The subscribed
method updates the db and defines two streams to be broadcasted across the channel, but the details are not very important since my question is a more general one:
- 如何设置测试来测试订阅所涉及的逻辑这个频道?
在测试控制器动作等类似交互时,RSpec 提供了许多辅助方法和各种实用程序,但我找不到有关 RSpec 和 ActionCable 的任何信息.
RSpec provides a lot of helper methods and various utilities when testing similar interactions like controller actions, but I couldn't find anything regarding RSpec and ActionCable.
推荐答案
您可能想要等待*https://github.com/rails/rails/pull/23211 合并.它添加了 ActionCable::TestCase.合并后,期待 rspec-rails 团队尽自己的一份力量:https://github.com/rspec/rspec-rails/issues/1606
You probably want to wait* for https://github.com/rails/rails/pull/23211 to be merged. It adds ActionCable::TestCase. Once that's merged, expect the rspec-rails team to do its part: https://github.com/rspec/rspec-rails/issues/1606
* 等待是可选的;您迫不及待地基于这个正在进行的工作"开展自己的工作,然后找到一个现在有效的解决方案.
* Waiting is optional; You can not wait and based your own work on this "work in progress" and find a solution that works right now.
这篇关于如何使用 RSpec 测试 ActionCable 频道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!