Class: GraphQL::Testing::MockActionCable

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/testing/mock_action_cable.rb

Overview

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

Defined Under Namespace

Classes: MockChannel, MockStream

Class Method Summary collapse

Class Method Details

.broadcast(stream_name, message) ⇒ Object

Implements Rails API



100
101
102
103
# File 'lib/graphql/testing/mock_action_cable.rb', line 100

def broadcast(stream_name, message)
  stream = @mock_streams[stream_name]
  stream && stream.mock_broadcast(message)
end

.clear_mocks ⇒ Object

Call this before each test run to make sure that MockActionCable's data is empty



90
91
92
# File 'lib/graphql/testing/mock_action_cable.rb', line 90

def clear_mocks
  @mock_streams = {}
end

.get_mock_channel ⇒ Object

Use this as context[:channel] to simulate an ActionCable channel

Returns

  • GraphQL::Testing::MockActionCable::MockChannel

:call-seq:

get_mock_channel() -> GraphQL::Testing::MockActionCable::MockChannel


118
119
120
# File 'lib/graphql/testing/mock_action_cable.rb', line 118

def get_mock_channel
  MockChannel.new
end

.mock_stream_for(stream_name) ⇒ Object

Used by mock code



106
107
108
# File 'lib/graphql/testing/mock_action_cable.rb', line 106

def mock_stream_for(stream_name)
  @mock_streams[stream_name] ||= MockStream.new
end

.mock_stream_names ⇒ Object

Returns

  • Array<String> — Streams that currently have subscribers

:call-seq:

mock_stream_names() -> Array[String]


128
129
130
# File 'lib/graphql/testing/mock_action_cable.rb', line 128

def mock_stream_names
  @mock_streams.keys
end

.server ⇒ Object

Implements Rails API



95
96
97
# File 'lib/graphql/testing/mock_action_cable.rb', line 95

def server
  self
end