Class: ActionCable::Channel::TestCase
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::TestCase ,
::ActiveSupport::Testing::Declarative ,
Minitest::Test
|
|
Instance Chain:
|
|
Inherits: |
ActiveSupport::TestCase
|
Defined in: | actioncable/lib/action_cable/channel/test_case.rb |
Overview
Superclass for Action Cable channel functional tests.
Basic example
Functional tests are written as follows:
-
First, one uses the
subscribe
method to simulate subscription creation. -
Then, one asserts whether the current state is as expected. “State” can be anything: transmitted messages, subscribed streams, etc.
For example:
class ChatChannelTest < ActionCable::Channel::TestCase
def test_subscribed_with_room_number
# Simulate a subscription creation
subscribe room_number: 1
# Asserts that the subscription was successfully created
assert subscription.confirmed?
# Asserts that the channel subscribes connection to a stream
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
subscribe
# Asserts that the subscription was rejected
assert subscription.rejected?
end
end
You can also perform actions:
def test_perform_speak
subscribe room_number: 1
perform :speak, message: "Hello, Rails!"
assert_equal "Hello, Rails!", transmissions.last["text"]
end
Special methods
TestCase
will also automatically provide the following instance methods for use in the tests:
- connection
-
An ActionCable::Channel::ConnectionStub, representing the current HTTP connection.
- subscription
-
An instance of the current channel, created when you call
subscribe
. - transmissions
-
A list of all messages that have been transmitted into the channel.
Channel is automatically inferred
TestCase
will automatically infer the channel under test from the test class name. If the channel cannot be inferred from the test class name, you can explicitly set it with tests
.
class SpecialEdgeCaseChannelTest < ActionCable::Channel::TestCase
tests SpecialChannel
end
Specifying connection identifiers
You need to set up your connection manually to provide values for the identifiers. To do this just use:
stub_connection(user: users(:john))
Testing broadcasting
TestCase
enhances ::ActionCable::TestHelper
assertions (e.g. assert_broadcasts
) to handle broadcasting to models:
# in your channel
def speak(data)
broadcast_to room, text: data["message"]
end
def test_speak
subscribe room_id: rooms(:chat).id
assert_broadcast_on(rooms(:chat), text: "Hello, Rails!") do
perform :speak, message: "Hello, Rails!"
end
end
Constant Summary
Behavior
- Attributes & Methods
- ._channel_class rw
- #_channel_class rw
- ._channel_class? ⇒ Boolean rw
- #_channel_class? ⇒ Boolean rw
- #connection readonly
- #subscription readonly
Class Attribute Summary
::ActiveSupport::TestCase
- Inherited
.file_fixture_path, .file_fixture_path?, | |
.test_order | Returns the order in which test cases are run. |
.test_order= | Sets the order in which test cases are run. |
Class Method Summary
::ActiveSupport::TestCase
- Inherited
.parallelize | Parallelizes the test suite. |
.parallelize_setup | Set up hook for parallel testing. |
.parallelize_teardown | Clean up hook for parallel testing. |
::ActiveSupport::Testing::Declarative
- Extended
Instance Attribute Summary
::ActionCable::TestHelper
- Included
::ActiveSupport::TestCase
- Inherited
Instance Method Summary
Behavior
- Included
#assert_broadcast_on, | |
#assert_broadcasts | Enhance TestHelper assertions to handle non-String broadcastings. |
#assert_has_stream | Asserts that the specified stream has been started. |
#assert_has_stream_for | Asserts that the specified stream for a model has started. |
#assert_no_streams | Asserts that no streams have been started. |
#perform | Perform action on a channel. |
#stub_connection | Set up test connection with the specified identifiers: |
#subscribe | Subscribe to the channel under test. |
#transmissions | Returns messages transmitted into channel. |
#unsubscribe | Unsubscribe the subscription under test. |
::ActionCable::TestHelper
- Included
#assert_broadcast_on | Asserts that the specified message has been sent to the stream. |
#assert_broadcasts | Asserts that the number of broadcasted messages to the stream matches the given number. |
#assert_no_broadcasts | Asserts that no messages have been sent to the stream. |
::ActiveSupport::TestCase
- Inherited
#assert_no_match, #assert_not_empty, #assert_not_equal, #assert_not_in_delta, #assert_not_in_epsilon, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_nil, #assert_not_operator, #assert_not_predicate, #assert_not_respond_to, #assert_not_same, | |
#assert_raise | test/unit backwards compatibility methods. |
#method_name |
::ActiveSupport::Testing::FileFixtures
- Included
#file_fixture | Returns a |
::ActiveSupport::Testing::TimeHelpers
- Included
#after_teardown, | |
#freeze_time | Calls |
#travel | Changes current time to the time in the future or in the past by a given time difference by stubbing |
#travel_back | Returns the current time back to its original state, by removing the stubs added by |
#travel_to | Changes current time to the given time by stubbing |
#unfreeze_time |
::ActiveSupport::Testing::Assertions
- Included
#assert_changes | Assertion that the result of evaluating an expression is changed before and after invoking the passed in block. |
#assert_difference | Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block. |
#assert_no_changes | Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block. |
#assert_no_difference | Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block. |
#assert_not | Asserts that an expression is not truthy. |
#assert_nothing_raised | Assertion that the block should not raise an exception. |
Class Attribute Details
._channel_class (rw)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 176
class_attribute :_channel_class
._channel_class? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 176
class_attribute :_channel_class
Instance Attribute Details
#_channel_class (rw)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 176
class_attribute :_channel_class
#_channel_class? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actioncable/lib/action_cable/channel/test_case.rb', line 176
class_attribute :_channel_class
#connection (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 178
attr_reader :connection, :subscription
#subscription (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/channel/test_case.rb', line 178
attr_reader :connection, :subscription