Skip to content

Commit d7c0a4a

Browse files
committed
feat: reify message when yielding
1 parent f692ba9 commit d7c0a4a

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

lib/pact/consumer_contract/message/contents.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'pact/reification'
12
module Pact
23
class ConsumerContract
34
class Message
@@ -15,10 +16,18 @@ def initialize contents
1516
end
1617

1718
def to_s
18-
if @contents.is_a?(Hash) || @contents.is_a?(Array)
19-
@contents.to_json
19+
if contents.is_a?(Hash) || contents.is_a?(Array)
20+
contents.to_json
2021
else
21-
@contents.to_s
22+
contents.to_s
23+
end
24+
end
25+
26+
def reified_contents_string
27+
if contents.is_a?(Hash) || contents.is_a?(Array)
28+
Pact::Reification.from_term(contents).to_json
29+
else
30+
Pact::Reification.from_term(contents).to_s
2231
end
2332
end
2433

lib/pact/message/consumer/consumer_contract_builder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ def is_expected_to_send(description)
2121
interaction_builder.is_expected_to_send(provider_state)
2222
end
2323

24-
def send_message
25-
# TODO reify content string
26-
yield @contents_string if block_given?
24+
def send_message_string
25+
yield contents.reified_contents_string if block_given?
2726
end
2827

2928
def handle_interaction_fully_defined(interaction)
29+
@contents = interaction.contents
3030
@contents_string = interaction.contents.to_s
3131
@interactions << interaction
3232
@interaction_builder = nil
@@ -42,7 +42,7 @@ def verify example_description
4242
private
4343

4444
attr_writer :interaction_builder
45-
attr_accessor :consumer_name, :provider_name, :consumer_contract_details
45+
attr_accessor :consumer_name, :provider_name, :consumer_contract_details, :contents
4646

4747
def interaction_builder
4848
@interaction_builder ||=

spec/features/create_message_pact_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
require "pact/message/consumer/rspec"
22
require "fileutils"
3+
require "pact/helpers"
34

45
RSpec.describe "creating a message pact" do
56

7+
include Pact::Helpers
8+
69
ZOO_PACT_FILE_PATH = "spec/pacts/zoo_consumer-zoo_provider.json"
710

811
before(:all) do
@@ -40,7 +43,7 @@ def call(content_string)
4043
.with_metadata(type: 'animal')
4144
.with_content(name: "Mary")
4245

43-
alice_producer.send_message do | content_string |
46+
alice_producer.send_message_string do | content_string |
4447
message_handler.call(content_string)
4548
end
4649

@@ -51,9 +54,9 @@ def call(content_string)
5154
alice_producer
5255
.given("there is an alligator named John")
5356
.is_expected_to_send("an alligator message")
54-
.with_content(name: "John")
57+
.with_content(name: like("John"))
5558

56-
alice_producer.send_message do | content_string |
59+
alice_producer.send_message_string do | content_string |
5760
message_handler.call(content_string)
5861
end
5962

spec/pacts/zoo_consumer-zoo_provider.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
},
3232
"matchingRules": {
3333
"body": {
34+
"$.name": {
35+
"match": "type"
36+
}
3437
}
3538
},
3639
"metaData": {

0 commit comments

Comments
 (0)