bartschotten / com-pact

An alternative Pact implementation for .NET
MIT License
16 stars 6 forks source link

Feature Request: WithBody Pass Object #11

Open AshleyPoole opened 4 years ago

AshleyPoole commented 4 years ago

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)));
bartschotten commented 4 years ago

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.

AshleyPoole commented 4 years ago

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.

bartschotten commented 4 years ago

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 ;)