14 ms·
> Hundreds of lines of mock code Mocking should be kept to a minimum and ideally avoided. Mocking is most useful when adding tests to legacy code, but new cod
by goto11 5y ago
> Hundreds of lines of mock code
Mocking should be kept to a minimum and ideally avoided.
Mocking is most useful when adding tests to legacy code, but new code should be designed so it can be tested without the need for mocking.
- adhesive_wombat 5y agoAlso can be pretty handy when there's hardware that needs something very specific to happen to work properly. Mocks can give you (some) confidence that when you poke the I2C bus, you won't hang it up. But it's very easy to overdo it and end up testing irrelevant implementation details instead.
- older 5y agoMocking own code should be avoided if possible, but it does not apply to external resources like database, http responses etc.
- simonw 5y agoI don't think it's ever a good idea to mock a database. Invest in a test framework that can performantly spin up a fresh database instance and throw it away at the end of the test run instead. Mocking outbound HTTP calls is worthwhile, but thankfully most testing frameworks I've used (at least in Python world) make that pretty easy.
- older 5y agoI mock database by using sqlite in-memory database. It is not 100% compatible with my production database but still god enough for most of my tests. This way I have fresh database for each unit test and the tests are still fast.