|
| 1 | +--- |
| 2 | +title: Messaging Pacts |
| 3 | +custom_edit_url: https://github.com/pact-foundation/pact-net/edit/master/docs/messaging-pacts.md |
| 4 | +--- |
| 5 | +<!-- This file has been synced from the pact-foundation/pact-net repository. Please do not edit it directly. The URL of the source file can be found in the custom_edit_url value above --> |
| 6 | + |
| 7 | +Messaging pacts work similarly to synchronous request/response pacts in that there is still a consumer |
| 8 | +and a producer. The consumer defines the messages it expects to receive (including metadata and the |
| 9 | +message contents) and the producer is verified to ensure that the messages it produces meet those |
| 10 | +expectations. |
| 11 | + |
| 12 | +It's important to ensure that any given consumer or provider name doesn't have both request/response and |
| 13 | +messaging pacts. If you have a HTTP API which also sends and/or receives messages, make sure the two different |
| 14 | +types use two different names, for example "Stock Broker API" and "Stock Broker Messaging". |
| 15 | + |
| 16 | +## Sample |
| 17 | + |
| 18 | +See the [sample](https://github.com/pact-foundation/pact-net/blob/master/samples/OrdersApi) for additional detail. |
| 19 | + |
| 20 | +## Consumer Tests |
| 21 | + |
| 22 | +Consumer tests are very similar to request/response pacts. Your consumer specifies which messages it wishes |
| 23 | +to receive and PactNet generates a pact file which contains all of the specified interactions. |
| 24 | + |
| 25 | +In code, this is: |
| 26 | + |
| 27 | +```csharp |
| 28 | +public class StockEventProcessorTests |
| 29 | +{ |
| 30 | + private readonly IMessagePactBuilderV4 messagePact; |
| 31 | + |
| 32 | + public StockEventProcessorTests(ITestOutputHelper output) |
| 33 | + { |
| 34 | + IPactV4 v4 = Pact.V4("Stock Event Consumer", "Stock Event Producer", new PactConfig |
| 35 | + { |
| 36 | + PactDir = "../../../pacts/", |
| 37 | + DefaultJsonSettings = new JsonSerializerOptions |
| 38 | + { |
| 39 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| 40 | + }, |
| 41 | + Outputters = new[] |
| 42 | + { |
| 43 | + new XUnitOutput(output) |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + this.messagePact = v4.WithMessageInteractions(); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void ReceiveSomeStockEvents() |
| 52 | + { |
| 53 | + this.messagePact |
| 54 | + .ExpectsToReceive("some stock ticker events") |
| 55 | + .Given("A list of events is pushed to the queue") |
| 56 | + .WithMetadata("key", "valueKey") |
| 57 | + .WithJsonContent(Match.MinType(new |
| 58 | + { |
| 59 | + Name = Match.Type("AAPL"), |
| 60 | + Price = Match.Decimal(1.23m), |
| 61 | + Timestamp = Match.Type(14.February(2022).At(13, 14, 15, 678)) |
| 62 | + }, 1)) |
| 63 | + .Verify<ICollection<StockEvent>>(events => |
| 64 | + { |
| 65 | + events.Should().BeEquivalentTo(new[] |
| 66 | + { |
| 67 | + new StockEvent |
| 68 | + { |
| 69 | + Name = "AAPL", |
| 70 | + Price = 1.23m, |
| 71 | + Timestamp = 14.February(2022).At(13, 14, 15, 678) |
| 72 | + } |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +After all of your consumer tests have passed a message pact file is written to disk. This file will be used |
| 80 | +during the provider verification tests. |
| 81 | + |
| 82 | +## Provider Tests |
| 83 | + |
| 84 | +Provider tests look very similar to request/response pacts, but with one big difference; you must register a |
| 85 | +handler for each interaction which generates a sample message. |
| 86 | + |
| 87 | +The key difference for mesaging pacts is that the transport used is not via HTTP. It would be unreasonable |
| 88 | +for PactNet to attempt to implement all the different transports that these messages could use - such as |
| 89 | +Kafka, RabbitMQ, ZeroMQ, etc - and so internally the messages are simulated during the provider verification |
| 90 | +stage. This is done transparently and so you don't need to worry about how this is achieved. |
| 91 | + |
| 92 | +In code, this is: |
| 93 | + |
| 94 | +```csharp |
| 95 | +public class StockEventGeneratorTests : IDisposable |
| 96 | +{ |
| 97 | + private readonly PactVerifier verifier; |
| 98 | + |
| 99 | + public StockEventGeneratorTests() |
| 100 | + { |
| 101 | + this.verifier = new PactVerifier("Stock Event Producer"); |
| 102 | + } |
| 103 | + |
| 104 | + public void Dispose() |
| 105 | + { |
| 106 | + // make sure you dispose the verifier to stop the internal messaging server |
| 107 | + GC.SuppressFinalize(this); |
| 108 | + this.verifier.Dispose(); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void EnsureEventApiHonoursPactWithConsumer() |
| 113 | + { |
| 114 | + string pactPath = Path.Combine("..", |
| 115 | + "..", |
| 116 | + "..", |
| 117 | + "..", |
| 118 | + "Consumer.Tests", |
| 119 | + "pacts", |
| 120 | + "Stock Event Consumer-Stock Event Producer.json"); |
| 121 | + |
| 122 | + var defaultSettings = new JsonSerializerOptions |
| 123 | + { |
| 124 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| 125 | + }; |
| 126 | + |
| 127 | + this.verifier |
| 128 | + .WithMessages(scenarios => |
| 129 | + { |
| 130 | + // register the responses to each interaction |
| 131 | + // the descriptions must match those in the pact file(s) |
| 132 | + scenarios.Add("a single event", () => new StockEvent |
| 133 | + { |
| 134 | + Name = "AAPL", |
| 135 | + Price = 1.23m |
| 136 | + }) |
| 137 | + .Add("some stock ticker events", builder => |
| 138 | + { |
| 139 | + builder.WithMetadata(new |
| 140 | + { |
| 141 | + ContentType = "application/json", |
| 142 | + Key = "value" |
| 143 | + }) |
| 144 | + .WithContent(new[] |
| 145 | + { |
| 146 | + new StockEvent { Name = "AAPL", Price = 1.23m }, |
| 147 | + new StockEvent { Name = "TSLA", Price = 4.56m } |
| 148 | + }); |
| 149 | + }); |
| 150 | + }, defaultSettings) |
| 151 | + .WithFileSource(new FileInfo(pactPath)) |
| 152 | + .Verify(); |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
0 commit comments