File: have_stream_spec.rb

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (182 lines) | stat: -rw-r--r-- 4,805 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require "rspec/rails/feature_check"

if RSpec::Rails::FeatureCheck.has_action_cable_testing?
  class StreamModel < Struct.new(:id)
    def to_gid_param
      "StreamModel##{id}"
    end
  end

  class StreamChannel < ActionCable::Channel::Base
    def self.channel_name
      "broadcast"
    end

    def subscribed
      stream_from "chat_#{params[:id]}" if params[:id]
      stream_for StreamModel.new(params[:user]) if params[:user]
    end
  end
end

RSpec.describe "have_stream matchers", skip: !RSpec::Rails::FeatureCheck.has_action_cable_testing?  do
  include RSpec::Rails::ChannelExampleGroup

  tests StreamChannel if respond_to?(:tests)

  before { stub_connection }

  describe "have_streams" do
    it "raises when no subscription started" do
      expect {
        expect(subscription).to have_streams
      }.to raise_error(/Must be subscribed!/)
    end

    it "does not allow usage" do
      subscribe

      expect {
        expect(subscription).to have_streams
      }.to raise_error(ArgumentError, /have_streams is used for negated expectations only/)
    end

    context "with negated form" do
      it "raises when no subscription started" do
        expect {
          expect(subscription).not_to have_streams
        }.to raise_error(/Must be subscribed!/)
      end

      it "raises ArgumentError when no subscription passed to expect" do
        subscribe id: 1

        expect {
          expect(true).not_to have_streams
        }.to raise_error(ArgumentError)
      end

      it "passes with negated form" do
        subscribe

        expect(subscription).not_to have_streams
      end

      it "fails with message" do
        subscribe id: 1

        expect {
          expect(subscription).not_to have_streams
        }.to raise_error(/expected not to have any stream started/)
      end
    end
  end

  describe "have_stream_from" do
    it "raises when no subscription started" do
      expect {
        expect(subscription).to have_stream_from("stream")
      }.to raise_error(/Must be subscribed!/)
    end

    it "raises ArgumentError when no subscription passed to expect" do
      subscribe id: 1

      expect {
        expect(true).to have_stream_from("stream")
      }.to raise_error(ArgumentError)
    end

    it "passes" do
      subscribe id: 1

      expect(subscription).to have_stream_from("chat_1")
    end

    it "fails with message" do
      subscribe id: 1

      expect {
        expect(subscription).to have_stream_from("chat_2")
      }.to raise_error(/expected to have stream "chat_2" started, but have \["chat_1"\]/)
    end

    context "with negated form" do
      it "passes" do
        subscribe id: 1

        expect(subscription).not_to have_stream_from("chat_2")
      end

      it "fails with message" do
        subscribe id: 1

        expect {
          expect(subscription).not_to have_stream_from("chat_1")
        }.to raise_error(/expected not to have stream "chat_1" started, but have \["chat_1"\]/)
      end
    end

    context "with composable matcher" do
      it "passes" do
        subscribe id: 1

        expect(subscription).to have_stream_from(a_string_starting_with("chat"))
      end

      it "fails with message" do
        subscribe id: 1

        expect {
          expect(subscription).to have_stream_from(a_string_starting_with("room"))
        }.to raise_error(/expected to have stream a string starting with "room" started, but have \["chat_1"\]/)
      end
    end
  end

  describe "have_stream_for" do
    it "raises when no subscription started" do
      expect {
        expect(subscription).to have_stream_for(StreamModel.new(42))
      }.to raise_error(/Must be subscribed!/)
    end

    it "raises ArgumentError when no subscription passed to expect" do
      subscribe user: 42

      expect {
        expect(true).to have_stream_for(StreamModel.new(42))
      }.to raise_error(ArgumentError)
    end

    it "passes" do
      subscribe user: 42

      expect(subscription).to have_stream_for(StreamModel.new(42))
    end

    it "fails with message" do
      subscribe user: 42

      expect {
        expect(subscription).to have_stream_for(StreamModel.new(31_337))
      }.to raise_error(/expected to have stream "broadcast:StreamModel#31337" started, but have \["broadcast:StreamModel#42"\]/)
    end

    context "with negated form" do
      it "passes" do
        subscribe user: 42

        expect(subscription).not_to have_stream_for(StreamModel.new(31_337))
      end

      it "fails with message" do
        subscribe user: 42

        expect {
          expect(subscription).not_to have_stream_for(StreamModel.new(42))
        }.to raise_error(/expected not to have stream "broadcast:StreamModel#42" started, but have \["broadcast:StreamModel#42"\]/)
      end
    end
  end
end