class GraphQL::Testing::MockActionCable
A stub implementation of ActionCable. Any methods to support the mock backend have mock in the name.
Examples
Example: Configuring your schema to use MockActionCable in the test environment
class MySchema < GraphQL::Schema # Use MockActionCable in test: use GraphQL::Subscriptions::ActionCableSubscriptions, action_cable: Rails.env.test? ? GraphQL::Testing::MockActionCable : ActionCable end
Example: Clearing old data before each test
setup do GraphQL::Testing::MockActionCable.clear_mocks end
Example: Using MockActionCable in a test case
# Create a channel to use in the test, pass it to GraphQL mock_channel = GraphQL::Testing::MockActionCable.get_mock_channel ActionCableTestSchema.execute("subscription { newsFlash { text } }", context: { channel: mock_channel }) # Trigger a subscription update ActionCableTestSchema.subscriptions.trigger(:news_flash, {}, {text: "After yesterday's rain, someone stopped on Rio Road to help a box turtle across five lanes of traffic"}) # Check messages on the channel expected_msg = { result: { "data" => { "newsFlash" => { "text" => "After yesterday's rain, someone stopped on Rio Road to help a box turtle across five lanes of traffic" } } }, more: true, } assert_equal [expected_msg], mock_channel.mock_broadcasted_messages
Public Class Methods
# File lib/graphql/testing/mock_action_cable.rb, line 99 def broadcast(stream_name, message) stream = @mock_streams[stream_name] stream && stream.mock_broadcast(message) end
Implements Rails API
Source
# File lib/graphql/testing/mock_action_cable.rb, line 89 def clear_mocks @mock_streams = {} end
Call this before each test run to make sure that MockActionCable’s data is empty
# File lib/graphql/testing/mock_action_cable.rb, line 117 def get_mock_channel MockChannel.new end
Use this as context[:channel] to simulate an ActionCable channel
Returns
# File lib/graphql/testing/mock_action_cable.rb, line 105 def mock_stream_for(stream_name) @mock_streams[stream_name] ||= MockStream.new end
Used by mock code
# File lib/graphql/testing/mock_action_cable.rb, line 127 def mock_stream_names @mock_streams.keys end
Returns
-
Array<String>— Streams that currently have subscribers
Source
# File lib/graphql/testing/mock_action_cable.rb, line 94 def server self end
Implements Rails API