How can I test ActionCable channels using RSpec?

后端 未结 4 1522
刺人心
刺人心 2021-01-01 19:25

I am wondering how to test ActionCable channels.

Let\'s say I have the following chat channel:

class ChatChannel < ApplicationCable::Channel
  def s         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 19:41

    You can use `action-cable-testing` gem.

    Add this to your Gemfile
    gem 'action-cable-testing'
    Then run
    $ bundle install

    Then add following spec

    # spec/channels/chat_channel_spec.rb

    require "rails_helper"
    
    RSpec.describe ChatChannel, type: :channel do
      before do
        # initialize connection with identifiers
        stub_connection current_user: current_user
      end
    
      it "rejects when no room id" do
        subscribe
        expect(subscription).to be_rejected
      end
    
      it "subscribes to a stream when room id is provided" do
        subscribe(chat_id: 42)
    
        expect(subscription).to be_confirmed
        expect(streams).to include("chat_42")
        expect(streams).to include("chat_stats_42")
      end
    end
    

    For more information see readme in github repo.

    https://github.com/palkan/action-cable-testing

    There are examples for both rspec and test_case

提交回复
热议问题