1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
Feature: `have_stream_from` matcher
The `have_stream_from` matcher is used to check if a channel has been subscribed to a given stream specified as a String.
If you use `stream_for` in you channel to subscribe to a model, use `have_stream_for` matcher instead.
The `have_no_streams` matcher is used to check if a channel hasn't been subscribed to any stream.
It is available only in channel specs.
Background:
Given action cable testing is available
And a file named "app/channels/chat_channel.rb" with:
"""ruby
class ChatChannel < ApplicationCable::Channel
def subscribed
reject unless params[:room_id].present?
stream_from "chat_#{params[:room_id]}"
end
def leave
stop_all_streams
end
end
"""
Scenario: Subscribing with params and checking streams
Given a file named "spec/channels/chat_channel_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe ChatChannel, type: :channel do
it "successfully subscribes" do
subscribe room_id: 42
expect(subscription).to be_confirmed
expect(subscription).to have_stream_from("chat_42")
end
end
"""
When I run `rspec spec/channels/chat_channel_spec.rb`
Then the example should pass
Scenario: Stopping all streams
Given a file named "spec/channels/chat_channel_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe ChatChannel, type: :channel do
it "successfully subscribes" do
subscribe(room_id: 42)
expect(subscription).to have_stream_from("chat_42")
perform :leave
expect(subscription).not_to have_streams
end
end
"""
When I run `rspec spec/channels/chat_channel_spec.rb`
Then the example should pass
Scenario: Subscribing and checking streams for models
Given a file named "app/channels/notifications_channel.rb" with:
"""ruby
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
end
end
"""
And a file named "app/channels/application_cable/connection.rb" with:
"""ruby
class ApplicationCable::Connection < ActionCable::Connection::Base
identified_by :current_user
end
"""
And a file named "app/models/user.rb" with:
"""ruby
class User < Struct.new(:name)
def to_gid_param
name
end
end
"""
And a file named "spec/channels/user_channel_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe NotificationsChannel, type: :channel do
it "successfully subscribes to user's stream" do
stub_connection current_user: User.new(42)
subscribe
expect(subscription).to be_confirmed
expect(subscription).to have_stream_for(User.new(42))
end
end
"""
When I run `rspec spec/channels/user_channel_spec.rb`
Then the example should pass
|