Hey, Looking for a way to share interaction setup...
# pact-net
e
Hey, Looking for a way to share interaction setup between our (consumer) contract tests and other functional tests in the consumer application in the form of
HttpRequestMessage
/
HttpResponseMessage
pairs. NB: I'm aware of
pact-stub-server
(link) however it's not quite what I'm looking for. i.e. assume we've got some shared code to define the "Foo" interaction
Copy code
private static void PopulateFooInteraction(IPactBuilderV3 pactBuilder)
{
    pactBuilder
        .UponReceiving("Request For Foo")
        .WithRequest(HttpMethod.Get, "/foo/endpoint")
        .WithHeader("x-correlation-id", "55dbb0b0-b0a0-4b2c-bee2-1cc77e8fba56")
        .WillRespond()
        .WithStatus(HttpStatusCode.OK)
        .WithHeader("Content-Type", "application/json")
        .WithJsonBody(Match.Type(new
        {
            Foo = "Bar"
        }));
}
Then we exercise the client code that depends on the Foo interaction - it calls
PopulateFooInteraction()
Copy code
[Fact]
public async Task CheckFooInteraction()
{
    var pact = Pact.V3("The.Consumer", "The.Provider", new PactConfig());
    var pactBuilder = pact.UsingNativeBackend();
    PopulateFooInteraction(pactBuilder);
    await PactBuilder.VerifyAsync(async ctx =>
    {
        // .. code that consumes the stubbed responses coming back for Foo
    });
}
Finally (somehow!) we harvest the messages implied by
PopulateFooInteraction()
This is the missing bit
Copy code
[Fact]
public void GetMessages()
{
    var messageCapture = new MessageCapture();
    PopulateFooInteraction(messageCapture);
    IEnumerable<(HttpRequestMessage, HttpResponseMessage)> messages = messageCapture.CollectInteractions();
}
The end goal being to use these messages with something like mockhttp.
m
Perhaps the simplest way would be to read back in the pact file, and programmatically create the
mockhttp
mocks?
t
What’s the use case here?
Ah, I think I understand - you want to tie the mocks in your functional tests together with the assertions in your contract tests?
💯 1
I’ve thought about doing something like that - I spoke about a rudimentary approach (using javascript) in the Pact virtual conference a couple of years ago - I’m travelling at the moment, but I can post a link later
I don’t think there’s existing tooling to do this easily, but it would be worth building.
I think one neat way to implement it might be to have a way to say “this interaction also triggers these state changes”
Then you could use the mock server for your functional tests
(And you might be able to use the mock client that way, too)