The most popular rust web frameworks, like actix_web, rocket, all seem to inject the database pool to the handler functions. That made me wonder how to do unit testing in those frameworks.
If you pass down the pool from the top to bottom, the only chance you'd have is to wrap the pool in a trait object and implement a mock sql pool yourself, returning the right objects when required. I assume most people won't write a sql engine to mock it.
Lazy static is the same story, if you access a global variable you cannot mock it for a specific method.
So what's left? Dyn Trait objects injected into the handler (service) which reference repositories (trait objects again). So when testing your service you can implement a mock repository returning the user you'd except in registration and it fails with "UsernameAlreadyExists" as expected. Problem: Only repositories have a reference to the DB Pool. How to start a transaction? How to share the transaction down to other service methods? Always have two methods? Create_user and create_user_in_transaction?
You could also inject DAO dyn Traits, but there's the same problems as above.
I'm sure I'm missing something. Is pool in request handler really the way to go? How do you handle transactions? And unit tests WITHOUT hitting the DB but only your mock repository?
It really feels like it's untestable.