does anyone know how to handle provider states for...
# pact-net
a
does anyone know how to handle provider states for running provider tests against the actual api? We're using Docker to run it as its own instance against a test db before running the tests.
m
Provider states require a test-only endpoint to be available. So if you’re running it in docker, you’d need to make sure that endpoint is available
it’s no different whether you’re running in docker or out of it
a
Can you run it without provider states? How do people normally test against the actual provider?
m
It’s not a recommended/common scenario.
Pact is best though of as a unit testing tool (it’s not, but that’s the mindset you should have)
You need to be able to test at a very granular level, which is hard to do when testing “outside” of the box.
Can you run it without provider states?
I don’t think there is a mode to disable them. But why would you want to do that? Then you wouldn’t be able to test those scenarios properly
a
so how would it actually test the provider for changes? If you're just testing an endpoint you created for the tests, you're not actually testing anything but a test
I'm trying to verify pacts that we have in Pactflow already
👍 1
m
The endpoint is the provider state handler endpoint, not a fake version of your API. You definitely need to test your real API (otherwise as you say, it’s not very useful)
👍 1
Maybe we’re talking across one another. What’s the actual problem you have?
a
I haven't seen any examples online of anything involving provider states that wasn't fake, unfortunately
which I think is where the disconnect is happening
m
can you point to an example? Perhaps I can then make sure it’s not misleading
m
So what’s fake about it? What happens is this: 1. Pact reads the pact file 2. It then loops through the interactions 3. For each interaction, it executes all provider states (i.e. test preconditions) a. This is where a request to the provider state endpoint is called to setup the provider into a state for the real request (to the actual endpoint) to work correctly. e.g. ensuring a user is in the database for a GET /users/:id 200 or ensuring a user is not in the database for a GET /users/:id 404 scenario) 4. The real request to the actual endpoint is sent 5. Pact checks the response
a
we have a separate call to seed data that is being done prior to this, I'm guessing that typically that's done by the provider state?
m
correct. What you’re suggesting is reasonable though if it covers all of the states you need
so I think what you’re asking is that tests not fail if the provider state endpoint is not setup/configured? That’s a reasonable ask
a
do the states ordinarily line up with the pact or is it just a standard add/remove data?
our strategy may have both been under and overthought
😆 1
m
the concept of a state is quite abstract - it could be configuring a mock, seeding data into a database etc. Whatever is required to get the system into the state needed for the test.
👍 1
So the strategy is less important. The state is best thought of as a pre-condition for the test, made explicit into the contract. If it actually doesn’t do anything, that’s also fine - the test is still clear “User A exists” or “The fulfilment system is unavailable” etc.
👍 1
a
does the provider state endpoint have to be on the provider or can it exist separately?
I think I'm finally starting to understand what is needed here, thanks
👍 1
m
does the provider state endpoint have to be on the provider or can it exist separately?
great question - the answer is that it doesn’t matter. It could be a separate test-only API that also can write to the underlying DB (if that’s the state that needs manipulation). Obviously, in-memory stubbing wouldn’t be effective this way, but I think you get the point
👍 1
a
gotcha, thanks for all of your help! you just spared me from a hefty migraine 🙂
m
No worries! I’ve got it on my list to update the docs page on provider states. This detail needs to be there Hopefully you can sleep easy now 😉
🙌 1