3 ms·
Just don't use them unless you have to. The two main reasons to mock are for network requests and because something is too slow. Other than that, test things fo
by globular-toast 6y ago
Just don't use them unless you have to. The two main reasons to mock are for network requests and because something is too slow. Other than that, test things for real. Do not isolate parts of your code from other parts of your code by using mocks. If your code does side effects on your own machine, like writes to the file system, let it write to a temporary directory.
I linked this excellent talk in another thread recently. I'll put it here again: https://www.youtube.com/watch?v=EZ05e7EMOLM https://www.youtube.com/watch?v=EZ05e7EMOLM
- scrollaway 6y agoIndeed. And if you're mocking eg. API calls in a client library, try to have tests for the real things as well. They don't have to be part of your normal test suite, they can run only if env vars are set with the API keys needed.
- tmarice 6y agoVCR.py (https://github.com/kevin1024/vcrpy https://github.com/kevin1024/vcrpy) is a great utility for mocking APIs. It will run each request once, save the responses to YAML files, and then replay the responses every time you re-run the tests. It's also very useful for caching API responses (e.g. you have a trial account with limited number of requests). Unfortunately, if used for testing, it will not cover the case when the original API changes its interface.
- mumblemumble 6y agoThat Ian Cooper talk is just fantastic. It's perhaps best contribution to the subject of TDD anyone has produced since Kent Beck popularized the idea in the first place.