com-pact icon indicating copy to clipboard operation
com-pact copied to clipboard

Feature Request: WithBody Pass Object

Open AshleyPoole opened this issue 5 years ago • 3 comments

If dealing with a complex object, it would make it easier writing tests if WithBody() as part of a response (WillRepondWith) chain could accept an object to be passed as the expected response for the verification. This would allow Com-Pact to operate similar to Pact.Net. Of course it's better to be very explicit with what is expected but allowing an object to be used as the expected response would allow for tests to be written much faster.

var expectedResponse = new ApiResponse { Id = "1234", SomeData = "Blah"};

_pactBuilder.SetUp(Pact.Interaction.Given(
                    $"An request for data")
                .UponReceiving("A get request for data")
                .With(Pact.Request.WithMethod(Method.GET)
                    .WithPath("/data"))
                .WillRespondWith(Pact.Response
                    .WithStatus((int) HttpStatusCode.OK)
                    .WithHeader("Content-Type", "application/json; charset=utf-8")
                    .WithBody(expectedResponse)));

AshleyPoole avatar May 18 '20 06:05 AshleyPoole

At the moment I see this as going too much against the philosophy of ComPact. As you say, it's better to be very explicit, or if the response doesn't matter, to just leave it out entirely.

But I'll leave this open to get a sense whether other people feel the same way.

bartschotten avatar May 21 '20 18:05 bartschotten

Thanks - The reasoning is because the client class under test, that issues the HTTP request, returns a strongly typed object, so for when writing tests, it would be nice to be able to instantiate an instance of that object and set the values to the expected. Also means if adding or renaming a field in the project, the PACT tests will auto be updated too, ready to be reviewed and committed.

AshleyPoole avatar Jun 03 '20 14:06 AshleyPoole

I see. but then again, if your able to share strongly typed objects between consumer and provider you don't really need Pact in the first place ;)

bartschotten avatar Jun 05 '20 12:06 bartschotten