Superclass for Action Cable channel functional tests.
Functional tests are written as follows:
First, one uses the
subscribemethod to simulate subscription creation.
Then, one asserts whether the current state is as expected. “State” can be anything: transmitted messages, subscribed streams, etc.
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
ActionCable::Channel::TestCase will also automatically provide the following instance methods for use in the tests:
ActionCable::Channel::ConnectionStub, representing the current HTTP connection.
An instance of the current channel, created when you call
A list of all messages that have been transmitted into the channel.
Channel is automatically inferred
ActionCable::Channel::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
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:
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