Skip to content

Commit 13bfb5c

Browse files
committed
chore: add demo of message example without kafka
1 parent 89754eb commit 13bfb5c

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
working-directory: example/animal-service
3636
- name: Test Pact-Ruby v2 Specs
3737
run: "bundle exec rake spec:v2"
38+
- name: Test Pact-Ruby v2 pact:specs
39+
run: "bundle exec rake pact:v2:spec"
3840
- name: Test Pact-Ruby v2 Zoo App Specs
3941
run: "bundle install && bundle exec rake spec:v2"
4042
if: matrix.ruby_version > '3.0'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class TestMessageConsumer
2+
3+
def consume_message(message)
4+
puts "Message consumed"
5+
puts message.to_json
6+
message
7+
end
8+
9+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class TestMessageProducer
2+
3+
def publish_message()
4+
message = {
5+
"email": "jane@example.com",
6+
"title": "Miss",
7+
"first_name": "Jane",
8+
"surname": "Doe"
9+
}
10+
end
11+
12+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require 'pact/v2'
4+
require 'pact/v2/rspec'
5+
require_relative '../../internal/app/producers/test_message_producer'
6+
7+
RSpec.describe 'Test Message Provider', :pact_v2 do
8+
message_pact_provider 'Test Message Producer', opts: {
9+
pact_dir: File.expand_path('../../pacts', __dir__),
10+
}
11+
12+
handle_message 'a customer created message' do |provider_state|
13+
body = TestMessageProducer.new.publish_message
14+
metadata = {}
15+
[body, metadata]
16+
end
17+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)