Module: PactV2ProducerDsl::ClassMethods

Defined in:
lib/pact/v2/rspec/support/pact_provider_helpers.rb

Constant Summary collapse

PACT_PROVIDER_NOT_DECLARED_MESSAGE =
"http_pact_provider or grpc_pact_provider should be declared first"

Instance Method Summary collapse

Instance Method Details

#_pact_provider(transport_type, provider, opts: {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 49

def _pact_provider(transport_type, provider, opts: {})
  raise "#{transport_type}_pact_provider is designed to be used with RSpec" unless defined?(::RSpec)
  raise "#{transport_type}_pact_provider has to be declared at the top level of a suite" unless top_level?
  raise "*_pact_provider is designed to be run once per provider so cannot be declared more than once" if defined?(@_pact_config)

  pact_config_instance = Pact::V2::Provider::PactConfig.new(transport_type, provider_name: provider, opts: opts)
  instance_variable_set(:@_pact_config, pact_config_instance)

  # rubocop:disable RSpec/BeforeAfterAll
  before(:context) do
    # rspec allows only context ivars in specs and ignores the rest
    # so we use block-as-a-closure feature to save pact_config ivar reference and make it available for descendants
    @_pact_config = pact_config_instance
  end
  # rubocop:enable RSpec/BeforeAfterAll

  it "verifies interactions with provider #{provider}" do
    pact_config.new_verifier.verify!
  end
end

#after_state_teardown(&block) ⇒ Object



75
76
77
78
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 75

def after_state_teardown(&block)
  raise PACT_PROVIDER_NOT_DECLARED_MESSAGE unless pact_config
  pact_config.after_teardown(&block)
end

#before_state_setup(&block) ⇒ Object



70
71
72
73
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 70

def before_state_setup(&block)
  raise PACT_PROVIDER_NOT_DECLARED_MESSAGE unless pact_config
  pact_config.before_setup(&block)
end

#execute_mixed_pact_provider(transport_type, provider, opts: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 26

def execute_mixed_pact_provider(transport_type, provider, opts: {})
  raise "#{transport_type}_pact_provider is designed to be used with RSpec" unless defined?(::RSpec)
  raise "#{transport_type}_pact_provider has to be declared at the top level of a suite" unless top_level?
  raise "mixed_pact_provider is designed to be run once per provider so cannot be declared more than once" if defined?(@_pact_config)

  pact_config_instance = Pact::V2::Provider::PactConfig.new(transport_type, provider_name: provider, opts: opts)
  instance_variable_set(:@_pact_config, pact_config_instance)

  # rubocop:disable RSpec/BeforeAfterAll
  before(:context) do
    # rspec allows only context ivars in specs and ignores the rest
    # so we use block-as-a-closure feature to save pact_config ivar reference and make it available for descendants
    @_pact_config = pact_config_instance
  end
  # rubocop:enable RSpec/BeforeAfterAll

  it "verifies mixed interactions with provider #{provider}" do
    pact_config.start_servers
    # todo: call any available verifier, or exit if none specified
    pact_config.http_config.new_verifier(@_pact_config).verify!
  end
end

#grpc_pact_provider(provider, opts: {}) ⇒ Object



14
15
16
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 14

def grpc_pact_provider(provider, opts: {})
  _pact_provider(:grpc, provider, opts: opts)
end

#handle_message(name, opts: {}, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 85

def handle_message(name, opts: {}, &block)
  async_klass = Pact::V2::Provider::PactConfig::Async
  if defined?(@_pact_config) &&
      @_pact_config.respond_to?(:async_config) &&
      @_pact_config.async_config.is_a?(async_klass)
    @_pact_config.async_config.new_message_handler(name, opts: opts, &block)
  elsif pact_config &&
      pact_config.respond_to?(:async_config) &&
      pact_config.async_config.is_a?(async_klass)
    pact_config.async_config.new_message_handler(name, opts: opts, &block)
  elsif defined?(@_pact_config) &&
      @_pact_config.is_a?(async_klass)
    @_pact_config.new_message_handler(name, opts: opts, &block)
  elsif pact_config.is_a?(async_klass)
    pact_config.new_message_handler(name, opts: opts, &block)

  else
    raise "handle_message can only be used with message_pact_provider or mixed_pact_provider with an async block"
  end
end

#http_pact_provider(provider, opts: {}) ⇒ Object



10
11
12
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 10

def http_pact_provider(provider, opts: {})
  _pact_provider(:http, provider, opts: opts)
end

#message_pact_provider(provider, opts: {}) ⇒ Object



18
19
20
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 18

def message_pact_provider(provider, opts: {})
  _pact_provider(:async, provider, opts: opts)
end

#mixed_pact_provider(provider, opts: {}) ⇒ Object



22
23
24
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 22

def mixed_pact_provider(provider, opts: {})
  execute_mixed_pact_provider(:mixed, provider, opts: opts)
end

#pact_configObject



106
107
108
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 106

def pact_config
  instance_variable_get(:@_pact_config)
end

#provider_state(name, opts: {}, &block) ⇒ Object



80
81
82
83
# File 'lib/pact/v2/rspec/support/pact_provider_helpers.rb', line 80

def provider_state(name, opts: {}, &block)
  raise PACT_PROVIDER_NOT_DECLARED_MESSAGE unless pact_config
  pact_config.new_provider_state(name, opts: opts, &block)
end