4 ms·
Why would you mock your own code, when you have your own real code right there?
by srtjstjsj 6y ago
Why would you mock your own code, when you have your own real code right there?
- hinkley 6y agoBecause that’s an integration test and I’m writing unit tests, which scale much higher.
- mypalmike 6y agoAs parent explained, if you use real objects, you end up testing the dependencies of those objects, and transitively the dependencies thereof, etc. For unit tests, the goal is to test the unit, not its dependencies. In practice, using real objects has many downsides including complex setup functions and a need to make changes to many layers of complex tests in order to implement even small code changes.
- SAI_Peregrinus 6y agoIn my case, I'm an embedded dev. So some of my own code is doing memory-mapped I/O to hardware, inline microcontroller ASM, or other stuff that can't physically happen on the system running the unit tests. So mocks (or fakes, it doesn't matter much here) are necessary to avoid running hardware-specific code on the wrong hardware. Of course I try to make those fakes at as low a level as possible, but there's still a substantial part of my code that can't be unit tested, and needs test programs in the actual firmware to check it.