pact-foundation / pact-net

.NET version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://pact.io
MIT License
842 stars 231 forks source link

Pact json being overwritten by each test in C# .NET #493

Closed cetk closed 7 months ago

cetk commented 7 months ago

Software versions

I'm writing Pact tests as part of a C# .NET application with NUnit test system. I have several functions that each run a Pact test. They overwrite the previous functions pact definition such that my final pact.json only has the last run test. How do I have separate test functions that all append to the pact test?

Steps to reproduce

Provide a repository, gist or reproducable code snippet so that we can test the problem. You may also want to adapt one of the examples in the repository to demonstrate the problem.

Relevent log files

Please ensure you set logging to DEBUG and attach any relevant log files here (or link from a gist).

mefellows commented 7 months ago

Did you mean to raise the issue over here: https://github.com/pact-foundation/pact-net/

cetk commented 7 months ago

yes

mefellows commented 7 months ago

Moving issue to the correct repo.

mefellows commented 7 months ago

Could you please perhaps demonstrate (via code) how you construct your Pact tests so that we can better understand?

You are also running quite an old version (3.0.0). Probably, you might just need to ensure you're calling the correct lifecycle method to write the pacts to file as you go.

Example: https://github.com/pact-foundation/pact-net/blob/3.0.2/Samples/EventApi/Consumer.Tests/EventsApiConsumerTests.cs.

If you're new to Pact, I'd suggest upgrading to the latest version.

cetk commented 7 months ago

the example above helps..it does have all tests published in same pact file, but I don't see any specific settings for the same. Is the latest version supports it ?

adamrodger commented 7 months ago

PactNet 4.x+ (which is the only supported version) works in merge mode only, thus running tests will always add to an existing file instead of overwriting it. This wasn't the case with v3.x and less, which are all unsupported versions now.

cetk commented 7 months ago

I see the builder has persisting pact file which keeps all interactions in single pact json. I have got one more question, how do I send request body with object with inner objects for pact test like

_mockProviderService.UponReceiving("a request to create a new event") .With(new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/events", Headers = new Dictionary<string, object> { { "Content-Type", "application/json; charset=utf-8" } }, Body = new { eventId, eventType = "DetailsView", timestamp = new TimeObject { date = "2024-02-25 21:38:57.290" } } }) .WillRespondWith(new ProviderServiceResponse { Status = 201 });