Sha256: 6114ae57188ae2f09f47ae5c37195c523fcd69881141674802066a09a3b0cda1

Contents?: true

Size: 1.95 KB

Versions: 11

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'hermes_messenger_of_the_gods/testing/array_endpoint'
require 'timeout'

module HermesMessengerOfTheGods
  module Testing
    module RSpecHelpers
      def mock_endpoints
        @mock_endpoints ||= Hash.new { |h, k| h[k] = {} }
      end

      def reset_all_mock_endpoints
        mock_endpoints.values.map(&:values).flatten.map(&:teardown)
        @mock_endpoints = nil
      end

      def fake_endpoint(type, endpoint, *args)
        klass = "HermesMessengerOfTheGods::Endpoints::#{type.to_s.camelize}".constantize

        build_fake_endpoint(klass, endpoint, *args).tap do |build_ep|
          allow(HermesMessengerOfTheGods::EndpointBuilder).to receive(:build)
            .with(anything, klass, endpoint, any_args).and_return(build_ep)
        end
      end

      def build_fake_endpoint(klass, endpoint, *args)
        endpoint = endpoint.to_s
        raise "Endpoint required" if endpoint == ''
        mock_endpoints[klass][endpoint].options.merge!(*args) if mock_endpoints[klass][endpoint]
        mock_endpoints[klass][endpoint] ||= Endpoints::Local.new(endpoint, "#{klass}::#{endpoint}", *args)
        mock_endpoints[klass][endpoint]
      end

      def setup_for_fake_endpoints!
        allow(HermesMessengerOfTheGods::EndpointBuilder).to receive(:build)
          .and_call_original
      end

      def fake_all_endpoints!
        allow(HermesMessengerOfTheGods::EndpointBuilder).to receive(:build) do |_, klass, endpoint, *args|
          build_fake_endpoint(klass, endpoint, *args)
        end
      end

      def wait_for_async_dispatches
        return if HermesMessengerOfTheGods.async_dispatches_in_progress.zero?

        Timeout.timeout(5) do
          sleep 0.01 until HermesMessengerOfTheGods.async_dispatches_in_progress.zero?
        end
      end
    end
  end
end

if defined?(RSpec)
  RSpec.configure do |config|
    config.before(:each) do
      setup_for_fake_endpoints!
      reset_all_mock_endpoints
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hermes_messenger_of_the_gods-2.4.0.pre2 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.4.0.pre1 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.3.4 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.3.3 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.3.2 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.3.1 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.3.0 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.2.0 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.1.1 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.1.0 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb
hermes_messenger_of_the_gods-2.0.0 lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb