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