Module: PactV2ConsumerDsl

Includes:
Pact::V2::Generators, Pact::V2::Matchers
Defined in:
lib/pact/v2/rspec/support/pact_consumer_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Pact::V2::Matchers

Pact::V2::Matchers::ANY_STRING_REGEX, Pact::V2::Matchers::ISO8601_REGEX, Pact::V2::Matchers::PACT_SPEC_V1, Pact::V2::Matchers::PACT_SPEC_V2, Pact::V2::Matchers::PACT_SPEC_V3, Pact::V2::Matchers::PACT_SPEC_V4, Pact::V2::Matchers::UUID_REGEX

Instance Method Summary collapse

Methods included from Pact::V2::Generators

#generate_date, #generate_datetime, #generate_from_provider_state, #generate_mock_server_url, #generate_random_boolean, #generate_random_decimal, #generate_random_hexadecimal, #generate_random_int, #generate_random_string, #generate_time, #generate_uuid

Methods included from Pact::V2::Matchers

#match_any_boolean, #match_any_decimal, #match_any_integer, #match_any_number, #match_any_string, #match_content_type, #match_date, #match_datetime, #match_each, #match_each_key, #match_each_kv, #match_each_regex, #match_each_value, #match_exactly, #match_include, #match_iso8601, #match_not_empty, #match_regex, #match_semver, #match_status_code, #match_time, #match_type_of, #match_uuid

Instance Method Details

#execute_http_pactObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pact/v2/rspec/support/pact_consumer_helpers.rb', line 60

def execute_http_pact
  raise InteractionBuilderError.new("interaction is designed to be used one-time only") if defined?(@used)
  mock_server = Pact::V2::Consumer::MockServer.create_for_http!(
    pact: pact_config.pact_handle, host: pact_config.mock_host, port: pact_config.mock_port
  )

  yield(mock_server)

ensure
  if mock_server.matched?
    mock_server.write_pacts!(pact_config.pact_dir)
  else
    msg = mismatches_error_msg(mock_server)
    raise Pact::V2::Consumer::HttpInteractionBuilder::InteractionMismatchesError.new(msg)
  end
  @used = true
  mock_server&.cleanup
  reset_pact
end

#mismatches_error_msg(mock_server) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/pact/v2/rspec/support/pact_consumer_helpers.rb', line 81

def mismatches_error_msg(mock_server)
  rspec_example_desc = RSpec.current_example&.description
  mismatches = JSON.pretty_generate(JSON.parse(mock_server.mismatches))
  mismatches_with_colored_keys = mismatches.gsub(/"([^"]+)":/) { |match| "\e[34m#{match}\e[0m" } # Blue keys / white values

  "#{rspec_example_desc} has mismatches: #{mismatches_with_colored_keys}"
end

#new_interaction(description = nil) ⇒ Object



48
49
50
# File 'lib/pact/v2/rspec/support/pact_consumer_helpers.rb', line 48

def new_interaction(description = nil)
  pact_config.new_interaction(description)
end

#pact_configObject



56
57
58
# File 'lib/pact/v2/rspec/support/pact_consumer_helpers.rb', line 56

def pact_config
  instance_variable_get(:@_pact_config)
end

#reset_pactObject

rubocop:disable Rails/Delegate



52
53
54
# File 'lib/pact/v2/rspec/support/pact_consumer_helpers.rb', line 52

def reset_pact # rubocop:disable Rails/Delegate
  pact_config.reset_pact
end