Module: HermesMessengerOfTheGods::Testing::RSpecHelpers

Defined in:
lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb

Instance Method Summary collapse

Instance Method Details

#build_fake_endpoint(klass, endpoint, *args) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 29

def build_fake_endpoint(klass, endpoint, *args)
  endpoint = endpoint.to_s
  raise 'Endpoint required' if endpoint == ''

  mock_endpoints[klass][endpoint]&.options&.merge!(*args)
  mock_endpoints[klass][endpoint] ||= Endpoints::Local.new(endpoint, "#{klass}::#{endpoint}", *args)
  mock_endpoints[klass][endpoint]
end

#fake_all_endpoints!Object



43
44
45
46
47
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 43

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

#fake_endpoint(type, endpoint, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 18

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

  allow(HermesMessengerOfTheGods::EndpointBuilder).to receive(:build)
    .with(anything, klass, endpoint, any_args) do |_, _, _, *new_args|
    build_fake_endpoint(klass, endpoint, *new_args)
  end

  build_fake_endpoint(klass, endpoint, *args)
end

#mock_endpointsObject



9
10
11
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 9

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

#reset_all_mock_endpointsObject



13
14
15
16
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 13

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

#setup_for_fake_endpoints!Object



38
39
40
41
# File 'lib/hermes_messenger_of_the_gods/testing/rspec_helpers.rb', line 38

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