Daniel Bunte
Feb 25, 2023

--

It's correct that this is the only way to mock external dependencies. However, think about this:

You're mocking behavior of an external library, thus you decouple your test code from the actual implementation. If the libraries behavior changes between versions, your test would still work, because they rely on the mock. But the real implementation might behave differently.

There's no rule that Unit Tests may not call external libraries directly :)

I also write Unit Tests that talk to databases, or in my current project, to a real MQTT broker. You can very easily set up docker containers that run with the tests. Check out the "testcontainers" crate for rust. This way, you can test the whole implementation and real behaviors.

--

--

Daniel Bunte

Leader, self-taught developer with interests in Architecture/Automation/Security.