aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable
diff options
context:
space:
mode:
authorSergey Ponomarev <me@sergey-ponomarev.ru>2018-12-18 16:48:14 -0500
committerSergey Ponomarev <me@sergey-ponomarev.ru>2018-12-18 17:37:53 -0500
commit9c8d4850f1d69db784ffafa93f38f780bf57c519 (patch)
treea3dd4b89de4123f3d483a4123d180dc19d7386ea /actioncable
parent2f6456cbe1da73c13b37e23720caa8716df85e78 (diff)
downloadrails-9c8d4850f1d69db784ffafa93f38f780bf57c519.tar.gz
rails-9c8d4850f1d69db784ffafa93f38f780bf57c519.tar.bz2
rails-9c8d4850f1d69db784ffafa93f38f780bf57c519.zip
Add streams assert methods to ActionCable channel test case
Diffstat (limited to 'actioncable')
-rw-r--r--actioncable/lib/action_cable/channel/test_case.rb49
-rw-r--r--actioncable/test/channel/test_case_test.rb30
2 files changed, 73 insertions, 6 deletions
diff --git a/actioncable/lib/action_cable/channel/test_case.rb b/actioncable/lib/action_cable/channel/test_case.rb
index dd2762ccd0..c4cf0ac0e7 100644
--- a/actioncable/lib/action_cable/channel/test_case.rb
+++ b/actioncable/lib/action_cable/channel/test_case.rb
@@ -84,7 +84,18 @@ module ActionCable
# assert subscription.confirmed?
#
# # Asserts that the channel subscribes connection to a stream
- # assert_equal "chat_1", streams.last
+ # assert_has_stream "chat_1"
+ #
+ # # Asserts that the channel subscribes connection to a specific
+ # # stream created for a model
+ # assert_has_stream_for Room.find(1)
+ # end
+ #
+ # def test_does_not_stream_with_incorrect_room_number
+ # subscribe room_number: -1
+ #
+ # # Asserts that not streams was started
+ # assert_no_streams
# end
#
# def test_does_not_subscribe_without_room_number
@@ -115,8 +126,6 @@ module ActionCable
# An instance of the current channel, created when you call `subscribe`.
# <b>transmissions</b>::
# A list of all messages that have been transmitted into the channel.
- # <b>streams</b>::
- # A list of all created streams subscriptions (as identifiers) for the subscription.
#
#
# == Channel is automatically inferred
@@ -167,7 +176,6 @@ module ActionCable
class_attribute :_channel_class
attr_reader :connection, :subscription
- delegate :streams, to: :subscription
ActiveSupport.run_load_hooks(:action_cable_channel_test_case, self)
end
@@ -251,6 +259,39 @@ module ActionCable
super(broadcasting_for(stream_or_object), *args)
end
+ # Asserts that no streams have been started.
+ #
+ # def test_assert_no_started_stream
+ # subscribe
+ # assert_no_streams
+ # end
+ #
+ def assert_no_streams
+ assert subscription.streams.empty?, "No streams started was expected, but #{subscription.streams.count} found"
+ end
+
+ # Asserts that the specified stream has been started.
+ #
+ # def test_assert_started_stream
+ # subscribe
+ # assert_has_stream 'messages'
+ # end
+ #
+ def assert_has_stream(stream)
+ assert subscription.streams.include?(stream), "Stream #{stream} has not been started"
+ end
+
+ # Asserts that the specified stream for a model has started.
+ #
+ # def test_assert_started_stream_for
+ # subscribe id: 42
+ # assert_has_stream_for User.find(42)
+ # end
+ #
+ def assert_has_stream_for(object)
+ assert_has_stream(broadcasting_for(object))
+ end
+
private
def check_subscribed!
raise "Must be subscribed!" if subscription.nil? || subscription.rejected?
diff --git a/actioncable/test/channel/test_case_test.rb b/actioncable/test/channel/test_case_test.rb
index 63d0d6207e..9c360d5dc3 100644
--- a/actioncable/test/channel/test_case_test.rb
+++ b/actioncable/test/channel/test_case_test.rb
@@ -93,13 +93,39 @@ class StreamsTestChannelTest < ActionCable::Channel::TestCase
def test_stream_without_params
subscribe
- assert_equal "test_0", streams.last
+ assert_has_stream "test_0"
end
def test_stream_with_params
subscribe id: 42
- assert_equal "test_42", streams.last
+ assert_has_stream "test_42"
+ end
+end
+
+class StreamsForTestChannel < ActionCable::Channel::Base
+ def subscribed
+ stream_for User.new(params[:id])
+ end
+end
+
+class StreamsForTestChannelTest < ActionCable::Channel::TestCase
+ def test_stream_with_params
+ subscribe id: 42
+
+ assert_has_stream_for User.new(42)
+ end
+end
+
+class NoStreamsTestChannel < ActionCable::Channel::Base
+ def subscribed; end # no-op
+end
+
+class NoStreamsTestChannelTest < ActionCable::Channel::TestCase
+ def test_stream_with_params
+ subscribe
+
+ assert_no_streams
end
end