Hello Team, What is the general notion while sett...
# general
s
Hello Team, What is the general notion while setting up provider states? For example - "A user John Doe with bank account exists", here should we actually create a user and link a bank account? Or is it possible to mock this behaviour?
m
Either is fine. If it's a db state you could seed the data. If it's a service layer you can mock it out. The important thing is the system is setup to receive and respond to the request and the key layers that are responsible for sending back the shape of the data are tested
Some choose to mock from the controller down. Some choose to test all the way to the boundary. Neither is right or wrong, but is contest dependent. I'd say start with mocking the data layer if that's convenient tho, as you'll get some additional functional test coverage as a nice side effect
t
I strongly prefer mocking the repository layer in your provider. The rationale is: • If you mock at the controller layer, you get no functional coverage. Pact is not about functional testing, but if you mock at the controller layer, all you know is that your provider can possibly marshal that response, not that it will. • If you create the data directly in the DB, you have to express the state in terms of DB data, which means that you then need a contract test between the provider state data and the actual data that is saved in the DB • If you create the data in the DB by calling endpoints / service methods in your provider, then it’s hard to know what went wrong when the tests fail • If you mock the repository layer in your service, you can express the provider state in terms of business logic - eg
A user John Doe with bank account exists -> when { userRepo.get('John Doe') } willReturn johnUserData
👍 3
s
Thanks for the answer both. That makes sense. But I'm trying to figure out how it would work with shared environments. For example - for unit tests the mocking is easy as it is being done in isolation. But with a shared environment (staging), other users would be affected if we mock our responses to particular http requests. 🤔 Or is my understanding still incorrect here? 😞
m
The answer is you don’t use a shared environment
For example - for unit tests the mocking is easy as it is being done in isolation.
exactly - this is why it’s preferable!
But with a shared environment (staging), other users would be affected if we mock our responses to particular http requests.
exactly - it’s one of the reasons it is not preferable to do it this way!
s
Perfect, thank you so much. This clears up a lot 🙂
👍 1