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:

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

Clearing old data before each test

setup do
  GraphQL::Testing::MockActionCable.clear_mocks
end

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



87
88
89
90
# File 'lib/graphql/testing/mock_action_cable.rb', line 87

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

.clear_mocksObject

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



77
78
79
# File 'lib/graphql/testing/mock_action_cable.rb', line 77

def clear_mocks
  @mock_streams = {}
end

.get_mock_channelGraphQL::Testing::MockActionCable::MockChannel

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



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

def get_mock_channel
  MockChannel.new
end

.mock_stream_for(stream_name) ⇒ Object

Used by mock code



93
94
95
# File 'lib/graphql/testing/mock_action_cable.rb', line 93

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

.mock_stream_namesArray<String>

Returns Streams that currently have subscribers.

Returns:

  • (Array<String>)

    Streams that currently have subscribers



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

def mock_stream_names
  @mock_streams.keys
end

.serverObject

Implements Rails API



82
83
84
# File 'lib/graphql/testing/mock_action_cable.rb', line 82

def server
  self
end