Re:Ask HN: Writing unit test mocks vs. real dependencies?
by goatcode on Wednesday October 21, 2020 @05:28PM
(#24852399)
Yes, I had a brain fart when I wrote my original comment. I think I should have meant to say mock dependencies (that we have created, not necessarily external ones, unless they're something like network calls), not inputs.
Re:Ask HN: Writing unit test mocks vs. real dependencies?
by dragontamer on Wednesday October 21, 2020 @06:16PM
(#24852845)
bitonicSort() is a hypothetical dependency. So at least, by my reading of the comment, it seems like you'd be recommending to mock out bitonicSort().
So you have:
So we're trying to test myFunc(). We have dependencies on bar() and bitonicSort(). There may be hidden dependencies on Baz class functions (maybe bar() calls Baz.func()).Now we have a variety of mocking strategies. We could mock the Baz class. We could mock bitonicSort(), we could mock bar() function.
But does such mocking really make our testing life easier? I argue it doesn't. Our unit test really should just be written as the following:
Easy peasy. No mocking needed. Compare now if we mocked out the Baz class, or the bitonic-sort method. It just becomes an unwieldy mess.There's no need to overthink things. Maybe have 2 or 3 different, reasonable, instantiations of Baz. If a bug is discovered in the future, create a Baz that represents that bug.
If bitonicSort isn't working correctly, its not our job (as writers of myFunc()) to care about bitonicSort's correctness. We are allowed to assume correctness upon our dependencies. It is also NOT our job to test bitonicSort. If we happen to catch a bitonicSort bug, that's good... but remember that bitonicSort will have unit tests of its own.