|
| 1 | +require 'pact/v2' |
| 2 | +require 'pact/v2/rspec' |
| 3 | +require_relative '../../../internal/app/consumers/test_message_consumer' |
| 4 | + |
| 5 | +describe TestMessageConsumer, :pact_v2 do |
| 6 | + has_message_pact_between 'Test Message Consumer', 'Test Message Provider' |
| 7 | + |
| 8 | + subject(:consumer) { TestMessageConsumer.new } |
| 9 | + |
| 10 | + describe 'Test Message Consumer' do |
| 11 | + # Notice that the expected message payload has fields which are different to that of the actual producer. |
| 12 | + # The TestMessageProducer actually sends a message with an additional 'title' field and a renamed 'surname' field. |
| 13 | + # See app/producers/test_message_producer.rb. |
| 14 | + |
| 15 | + # before do |
| 16 | + # # Here we are calling test_message_producer, which is mocking the actual TestMessageProducer defined in app/producers/test_message_producer.rb. |
| 17 | + # # In pact-message we use mocked providers in consumer side tests. These are defined in a similar way to mocked APIs/service providers in standard HTTP CDCT. |
| 18 | + # # See spec/support/pact_spec_helper.rb. |
| 19 | + # let(:expected_payload) |
| 20 | + # { |
| 21 | + # "email": match_type_of('jane@example.com'), |
| 22 | + # "first_name": match_type_of('Jane') |
| 23 | + # # "last_name": Pact.like("Doe") # uncomment to see failure in provider code |
| 24 | + # } |
| 25 | + |
| 26 | + # let(:interaction) do |
| 27 | + # new_interaction.given('A customer is created') |
| 28 | + # .upon_receiving('a customer created message') |
| 29 | + # # .with_metadata() |
| 30 | + # # .with_json_contents(match_type_of(expected_payload)) |
| 31 | + # .with_json_contents(expected_payload) |
| 32 | + # end |
| 33 | + # end |
| 34 | + |
| 35 | + # This test is a bit redundant, it's essentially marking our own homework and will always pass. |
| 36 | + # However IRL the consumer would probably be doing something more complex which we could assert on. |
| 37 | + # See spec/pacts/test_message_consumer-test_message_producer.json for the generated contract file. |
| 38 | + # Note that this contract does not match what the producer outputs in app/producers/test_message_producer.rb.. |
| 39 | + # If we were to run producer side verification on this contract, it should fail. |
| 40 | + # This failure would indicate a mismatch between the consumers expectations of the message format and what the producer actually sends. |
| 41 | + |
| 42 | + let(:expected_payload) do |
| 43 | + { |
| 44 | + "email": match_type_of('jane@example.com'), |
| 45 | + "first_name": match_type_of('Jane') |
| 46 | + # "last_name": Pact.like("Doe") # uncomment to see failure in provider code |
| 47 | + } |
| 48 | + end |
| 49 | + |
| 50 | + let(:interaction) do |
| 51 | + new_interaction.given('A customer is created') |
| 52 | + .upon_receiving('a customer created message') |
| 53 | + # .with_metadata() |
| 54 | + # .with_json_contents(match_type_of(expected_payload)) |
| 55 | + .with_json_contents(expected_payload) |
| 56 | + end |
| 57 | + |
| 58 | + it 'Successfully consumes the message and creates a pact contract file' do |
| 59 | + interaction.execute do |json_payload, _meta| |
| 60 | + @message = consumer.consume_message(json_payload) |
| 61 | + expect(@message).to eq(json_payload) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments