1515 pact_specification_version '2'
1616 end
1717 end
18+
19+ has_pact_with "Wiffle Provider" do
20+ builder :wiffle_producer do
21+ pact_specification_version '2'
22+ end
23+ end
1824 end
1925
2026 FileUtils . rm_rf ZOO_PACT_FILE_PATH
2127 end
2228
23- class MessageHandler
24-
29+ class StringMessageHandler
2530 attr_reader :output_stream
2631
2732 def initialize
@@ -34,32 +39,109 @@ def call(content_string)
3439 end
3540 end
3641
37- let ( :message_handler ) { MessageHandler . new }
42+ class HashSymbolMessageHandler
43+ attr_reader :output_stream
3844
39- it "allows a consumer to test that it can handle a message example correctly" , pact : :message do
40- alice_producer
41- . given ( "there is an alligator named Mary" )
42- . is_expected_to_send ( "an alligator message" )
43- . with_metadata ( type : 'animal' )
44- . with_content ( name : "Mary" )
45+ def initialize
46+ @output_stream = StringIO . new
47+ end
4548
46- alice_producer . send_message_string do | content_string |
47- message_handler . call ( content_string )
49+ def call ( content_hash )
50+ output_stream . print "Hello #{ content_hash [ :name ] } "
4851 end
52+ end
4953
50- expect ( message_handler . output_stream . string ) . to eq ( "Hello Mary" )
54+ class HashStringMessageHandler
55+ attr_reader :output_stream
56+
57+ def initialize
58+ @output_stream = StringIO . new
59+ end
60+
61+ def call ( content_hash )
62+ output_stream . print "Hello #{ content_hash [ 'name' ] } "
63+ end
5164 end
5265
53- it "allows a consumer to test that it can handle a second message example correctly" , pact : :message do
54- alice_producer
55- . given ( "there is an alligator named John" )
56- . is_expected_to_send ( "an alligator message" )
57- . with_content ( name : like ( "John" ) )
66+ class ArrayMessageHandler
67+ attr_reader :output_stream
68+
69+ def initialize
70+ @output_stream = StringIO . new
71+ end
5872
59- alice_producer . send_message_string do | content_string |
60- message_handler . call ( content_string )
73+ def call ( array )
74+ output_stream . print "Hello #{ array . join ( ", " ) } "
6175 end
76+ end
77+
78+ context "with a string message" do
79+ let ( :message_handler ) { StringMessageHandler . new }
80+
81+ it "allows a consumer to test that it can handle the expected message" , pact : :message do
82+ alice_producer
83+ . given ( "there is an alligator named Mary" )
84+ . is_expected_to_send ( "an alligator message" )
85+ . with_metadata ( type : 'animal' )
86+ . with_content ( name : "Mary" )
6287
63- expect ( message_handler . output_stream . string ) . to eq ( "Hello John" )
88+ alice_producer . send_message_string do | content_string |
89+ message_handler . call ( content_string )
90+ end
91+
92+ expect ( message_handler . output_stream . string ) . to eq ( "Hello Mary" )
93+ end
94+ end
95+
96+ context "with a hash message with symbol keys" do
97+ let ( :message_handler ) { HashSymbolMessageHandler . new }
98+
99+ it "allows a consumer to test that it can handle the expected message" , pact : :message do
100+ alice_producer
101+ . given ( "there is an alligator named John" )
102+ . is_expected_to_send ( "an alligator message" )
103+ . with_content ( name : like ( "John" ) )
104+
105+ alice_producer . send_message_hash do | content_hash |
106+ message_handler . call ( content_hash )
107+ end
108+
109+ expect ( message_handler . output_stream . string ) . to eq ( "Hello John" )
110+ end
111+ end
112+
113+ context "with a hash message with string keys" do
114+ let ( :message_handler ) { HashStringMessageHandler . new }
115+
116+ it "allows a consumer to test that it can handle the expected message" , pact : :message do
117+ alice_producer
118+ . given ( "there is an alligator named Sue" )
119+ . is_expected_to_send ( "an alligator message" )
120+ . with_content ( "name" => like ( "Sue" ) )
121+
122+ alice_producer . send_message_hash do | content_hash |
123+ message_handler . call ( content_hash )
124+ end
125+
126+ expect ( message_handler . output_stream . string ) . to eq ( "Hello Sue" )
127+ end
128+ end
129+
130+ context "with an array message" do
131+ let ( :message_handler ) { ArrayMessageHandler . new }
132+
133+ it "allows a consumer to test that it can handle the expected message" , pact : :message do
134+ alice_producer
135+ . given ( "there is an alligator named John" , { some : "params" } )
136+ . and ( "there is an alligator named Mary" )
137+ . is_expected_to_send ( "an alligator message" )
138+ . with_content ( [ like ( "John" ) , like ( "Mary" ) ] )
139+
140+ alice_producer . send_message_hash do | content_hash |
141+ message_handler . call ( content_hash )
142+ end
143+
144+ expect ( message_handler . output_stream . string ) . to eq ( "Hello John, Mary" )
145+ end
64146 end
65147end
0 commit comments