Skip to content

Commit d9e2b15

Browse files
Add assertions for stopped ActionCable streams
This adds two new assertion methods for ActionCable test cases: `assert_not_has_stream` and `assert_not_has_stream_for`. These methods can be used to assert that a stream has been stopped, e.g. via `stop_stream` or `stop_stream_for`.
1 parent 64370c9 commit d9e2b15

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

actioncable/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1+
* Add two new assertion methods for ActionCable test cases: `assert_not_has_stream`
2+
and `assert_not_has_stream_for`. These methods can be used to assert that a
3+
stream has been stopped, e.g. via `stop_stream` or `stop_stream_for`. They complement
4+
the already existing `assert_has_stream` and `assert_has_stream_for` methods.
5+
6+
```ruby
7+
assert_not_has_stream "messages"
8+
assert_not_has_stream_for User.find(42)
9+
```
10+
11+
*Sebastian Pöll*
112

213
Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/actioncable/CHANGELOG.md) for previous changes.

actioncable/lib/action_cable/channel/test_case.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ def assert_has_stream_for(object)
312312
assert_has_stream(broadcasting_for(object))
313313
end
314314

315+
# Asserts that the specified stream has not been started.
316+
#
317+
# def test_assert_not_started_stream
318+
# subscribe
319+
# assert_not_has_stream 'messages'
320+
# end
321+
#
322+
def assert_not_has_stream(stream)
323+
assert subscription.streams.exclude?(stream), "Stream #{stream} has been started"
324+
end
325+
326+
# Asserts that the specified stream for a model has not started.
327+
#
328+
# def test_assert_not_started_stream_for
329+
# subscribe id: 41
330+
# assert_not_has_stream_for User.find(42)
331+
# end
332+
#
333+
def assert_not_has_stream_for(object)
334+
assert_not_has_stream(broadcasting_for(object))
335+
end
336+
315337
private
316338
def check_subscribed!
317339
raise "Must be subscribed!" if subscription.nil? || subscription.rejected?

actioncable/test/channel/test_case_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ def test_stream_with_params
107107
assert_has_stream "test_42"
108108
end
109109

110+
def test_not_stream_without_params
111+
subscribe
112+
unsubscribe
113+
114+
assert_not_has_stream "test_0"
115+
end
116+
117+
def test_not_stream_with_params
118+
subscribe id: 42
119+
perform :unsubscribed, id: 42
120+
121+
assert_not_has_stream "test_42"
122+
end
123+
110124
def test_unsubscribe_from_stream
111125
subscribe
112126
unsubscribe
@@ -119,6 +133,10 @@ class StreamsForTestChannel < ActionCable::Channel::Base
119133
def subscribed
120134
stream_for User.new(params[:id])
121135
end
136+
137+
def unsubscribed
138+
stop_stream_for User.new(params[:id])
139+
end
122140
end
123141

124142
class StreamsForTestChannelTest < ActionCable::Channel::TestCase
@@ -127,6 +145,13 @@ def test_stream_with_params
127145

128146
assert_has_stream_for User.new(42)
129147
end
148+
149+
def test_not_stream_with_params
150+
subscribe id: 42
151+
perform :unsubscribed, id: 42
152+
153+
assert_not_has_stream_for User.new(42)
154+
end
130155
end
131156

132157
class NoStreamsTestChannel < ActionCable::Channel::Base

0 commit comments

Comments
 (0)