Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test ActionCable channels using RSpec?

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

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:

  • How can I set up a test to test the logic involved by subscribing to this channel?

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.

like image 396
Dani Avatar asked Feb 05 '16 15:02

Dani


People also ask

How do you test an action cable?

Testing Action Cable To test this connection class, we can use connect method to simulate a client server connection and then test the state of connection is as expected. The connection object is available in the test.

What are RSpec tests?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


1 Answers

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.

like image 185
Leonel Galán Avatar answered Oct 20 '22 03:10

Leonel Galán