aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test
diff options
context:
space:
mode:
authorVladimir Dementyev <dementiev.vm@gmail.com>2018-08-19 19:23:33 -0400
committerVladimir Dementyev <dementiev.vm@gmail.com>2018-08-19 19:23:33 -0400
commitc11ca0996227ef6f3b6154e4742a9e03949557df (patch)
treed13798731e23d3f67db9dc895c37be58365a7a5f /actioncable/test
parentf7dd2d67d6a8b74f2762be51dff4f96983175aee (diff)
downloadrails-c11ca0996227ef6f3b6154e4742a9e03949557df.tar.gz
rails-c11ca0996227ef6f3b6154e4742a9e03949557df.tar.bz2
rails-c11ca0996227ef6f3b6154e4742a9e03949557df.zip
Add Action Cable test case and test helper
Diffstat (limited to 'actioncable/test')
-rw-r--r--actioncable/test/test_helper.rb4
-rw-r--r--actioncable/test/test_helper_test.rb106
2 files changed, 110 insertions, 0 deletions
diff --git a/actioncable/test/test_helper.rb b/actioncable/test/test_helper.rb
index ac7881c950..f5b9ebf517 100644
--- a/actioncable/test/test_helper.rb
+++ b/actioncable/test/test_helper.rb
@@ -15,6 +15,10 @@ end
# Require all the stubs and models
Dir[File.expand_path("stubs/*.rb", __dir__)].each { |file| require file }
+# Set test adapter and logger
+ActionCable.server.config.cable = { "adapter" => "test" }
+ActionCable.server.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
+
class ActionCable::TestCase < ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
diff --git a/actioncable/test/test_helper_test.rb b/actioncable/test/test_helper_test.rb
new file mode 100644
index 0000000000..f82adb9c8f
--- /dev/null
+++ b/actioncable/test/test_helper_test.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class BroadcastChannel < ActionCable::Channel::Base
+end
+
+class TransmissionsTest < ActionCable::TestCase
+ def test_assert_broadcasts
+ assert_nothing_raised do
+ assert_broadcasts("test", 1) do
+ ActionCable.server.broadcast "test", "message"
+ end
+ end
+ end
+
+ def test_assert_broadcasts_with_no_block
+ assert_nothing_raised do
+ ActionCable.server.broadcast "test", "message"
+ assert_broadcasts "test", 1
+ end
+
+ assert_nothing_raised do
+ ActionCable.server.broadcast "test", "message 2"
+ ActionCable.server.broadcast "test", "message 3"
+ assert_broadcasts "test", 3
+ end
+ end
+
+ def test_assert_no_broadcasts_with_no_block
+ assert_nothing_raised do
+ assert_no_broadcasts "test"
+ end
+ end
+
+ def test_assert_no_broadcasts
+ assert_nothing_raised do
+ assert_no_broadcasts("test") do
+ ActionCable.server.broadcast "test2", "message"
+ end
+ end
+ end
+
+ def test_assert_broadcasts_message_too_few_sent
+ ActionCable.server.broadcast "test", "hello"
+ error = assert_raises Minitest::Assertion do
+ assert_broadcasts("test", 2) do
+ ActionCable.server.broadcast "test", "world"
+ end
+ end
+
+ assert_match(/2 .* but 1/, error.message)
+ end
+
+ def test_assert_broadcasts_message_too_many_sent
+ error = assert_raises Minitest::Assertion do
+ assert_broadcasts("test", 1) do
+ ActionCable.server.broadcast "test", "hello"
+ ActionCable.server.broadcast "test", "world"
+ end
+ end
+
+ assert_match(/1 .* but 2/, error.message)
+ end
+end
+
+class TransmitedDataTest < ActionCable::TestCase
+ include ActionCable::TestHelper
+
+ def test_assert_broadcast_on
+ assert_nothing_raised do
+ assert_broadcast_on("test", "message") do
+ ActionCable.server.broadcast "test", "message"
+ end
+ end
+ end
+
+ def test_assert_broadcast_on_with_hash
+ assert_nothing_raised do
+ assert_broadcast_on("test", text: "hello") do
+ ActionCable.server.broadcast "test", text: "hello"
+ end
+ end
+ end
+
+ def test_assert_broadcast_on_with_no_block
+ assert_nothing_raised do
+ ActionCable.server.broadcast "test", "hello"
+ assert_broadcast_on "test", "hello"
+ end
+
+ assert_nothing_raised do
+ ActionCable.server.broadcast "test", "world"
+ assert_broadcast_on "test", "world"
+ end
+ end
+
+ def test_assert_broadcast_on_message
+ ActionCable.server.broadcast "test", "hello"
+ error = assert_raises Minitest::Assertion do
+ assert_broadcast_on("test", "world")
+ end
+
+ assert_match(/No messages sent/, error.message)
+ end
+end