-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcreate_message_pact_spec.rb
148 lines (114 loc) · 3.88 KB
/
create_message_pact_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require "pact/message/consumer/rspec"
require "fileutils"
require "pact/helpers"
require "ostruct"
RSpec.describe "creating a message pact" do
include Pact::Helpers
ZOO_PACT_FILE_PATH = "spec/pacts/zoo_consumer-zoo_provider.json"
before(:all) do
Pact.message_consumer "Zoo Consumer" do
has_pact_with "Zoo Provider" do
mock_provider :alice_producer do
pact_specification_version '2'
end
end
has_pact_with "Wiffle Provider" do
mock_provider :wiffle_producer do
pact_specification_version '2'
end
end
end
FileUtils.rm_rf ZOO_PACT_FILE_PATH
end
class StringMessageHandler
attr_reader :output_stream
def initialize
@output_stream = StringIO.new
end
def call(content_string)
message = OpenStruct.new(JSON.parse(content_string))
output_stream.print "Hello #{message.name}"
end
end
class HashSymbolMessageHandler
attr_reader :output_stream
def initialize
@output_stream = StringIO.new
end
def call(content_hash)
output_stream.print "Hello #{content_hash[:name]}"
end
end
class HashStringMessageHandler
attr_reader :output_stream
def initialize
@output_stream = StringIO.new
end
def call(content_hash)
output_stream.print "Hello #{content_hash['name']}"
end
end
class ArrayMessageHandler
attr_reader :output_stream
def initialize
@output_stream = StringIO.new
end
def call(array)
output_stream.print "Hello #{array.join(", ")}"
end
end
context "with a string message" do
let(:message_handler) { StringMessageHandler.new }
it "allows a consumer to test that it can handle the expected message", pact: :message do
alice_producer
.given("there is an alligator named Mary")
.is_expected_to_send("an alligator message")
.with_metadata(type: 'animal')
.with_content(name: "Mary")
alice_producer.send_message_string do | content_string |
message_handler.call(content_string)
end
expect(message_handler.output_stream.string).to eq ("Hello Mary")
end
end
context "with a hash message with symbol keys" do
let(:message_handler) { HashSymbolMessageHandler.new }
it "allows a consumer to test that it can handle the expected message", pact: :message do
alice_producer
.given("there is an alligator named John")
.is_expected_to_send("an alligator message")
.with_content(name: like("John"))
alice_producer.send_message_hash do | content_hash |
message_handler.call(content_hash)
end
expect(message_handler.output_stream.string).to eq ("Hello John")
end
end
context "with a hash message with string keys" do
let(:message_handler) { HashStringMessageHandler.new }
it "allows a consumer to test that it can handle the expected message", pact: :message do
alice_producer
.given("there is an alligator named Sue")
.is_expected_to_send("an alligator message")
.with_content("name" => like("Sue"))
alice_producer.send_message_hash do | content_hash |
message_handler.call(content_hash)
end
expect(message_handler.output_stream.string).to eq ("Hello Sue")
end
end
context "with an array message" do
let(:message_handler) { ArrayMessageHandler.new }
it "allows a consumer to test that it can handle the expected message", pact: :message do
alice_producer
.given("there is an alligator named John", { some: "params" })
.and("there is an alligator named Mary")
.is_expected_to_send("an alligator message")
.with_content([like("John"), like("Mary")])
alice_producer.send_message_hash do | content_hash |
message_handler.call(content_hash)
end
expect(message_handler.output_stream.string).to eq ("Hello John, Mary")
end
end
end