pact-foundation / pact-workshop-dotnet-core-v1

A workshop for Pact using .NET Core
Apache License 2.0
76 stars 209 forks source link

How to inject a mocked repository into the provider via the middleware (.net core) #9

Open pedramp20 opened 5 years ago

pedramp20 commented 5 years ago

In cases where the provider state needs to be set prior to the test (For instance when an entity needs to exist in the database with a specific id), what are the best practices to update the state of the provider? From my perspective, there should be a way to mock the repository so that data can be manipulated easily and quickly, if this is the right approach, how is it possible to inject a mocked repository into the provider? Since, the test does not run the server and an instance of the Provider API needs to be run independently, I can't get my head around a solution, which allows dynamic injection of a mocked repository. If this is not possible, then how is it possible to update the state in the Provider? Does it mean that the provider endpoints (i.e. POST) should be called or data should be inserted directly to the database from the middleware? Even if these approaches are implemented, the Id of the created object is auto incremented and would not match the contract. Does it mean that I have to use Matching in the contract? What is the best practice to implement tear down and cleanup the changes then?