3 ms·
I think about unit tests being useful for getting more confidence that some deterministic, pure (mathematically speaking) and stateless piece of code that's dat
by davnicwil 3y ago
I think about unit tests being useful for getting more confidence that some deterministic, pure (mathematically speaking) and stateless piece of code that's data in data out actually works, particularly when you change it.
If any of those conditions doesn't hold the cost/benefit certainly and even sometimes the absolute utility goes way down.
If I have to mock anything, in particular, or more generally care at all about any implementation details (ie side effects) then I just think might as well make this a full on automated functional test then.
As soon as fake code is introduced into the test its utility rapidly decays in time as the things it fakes themselves change.
- corey 3y agoI agree that mocks are brittle and nearly useless. If you follow SOLID principles to the extreme, you'll find that your code is separated into logic code that is pure and easy to unit test, and IO code that is very simple and can be tested by a relatively few number of integration tests.
- aleksiy123 3y agoTo some extent this is pretty much the same as mocking. You are still injecting fake data into your pure logic functions whether its through their parameters or by them calling a mock. I agree preferable but sometimes you want to test the logic of the code thats actually making decisions about how and when the IO is called. You can do it with integration tests of course but in more complex environments with lots of complex IO dependencies mocking is cheaper. Its also hard to simulate specific failures in integration tests like a specific request failing. Pretty much mocking with extra steps. So mocking has its place as well.