4 ms·
Fakes > Mocks! Post: https://tyrrrz.me/blog/fakes-over-mocks https://tyrrrz.me/blog/fakes-over-mocks Previous HN Discussion on the above post: https://news.yc
by rockwotj 4y ago
Fakes > Mocks!
Post: https://tyrrrz.me/blog/fakes-over-mocks https://tyrrrz.me/blog/fakes-over-mocks
Previous HN Discussion on the above post: https://news.ycombinator.com/item?id=24770954 https://news.ycombinator.com/item?id=24770954
This does require dependency injection (DI), and my hot take is that manual DI > framework magic. Sure it's a little extra code writing, but folks should optimize for reading code, and some of the frameworks out there have a lot of complex concepts for DI (For example dagger is a popular DI framework for JVM, and the dev-guide has a lot of stuff in it, which feels like a lot to learn for what it's providing: https://dagger.dev/dev-guide/ https://dagger.dev/dev-guide/, no knock against dagger, I've ran into this with other DI frameworks too!)
- sethammons 4y agoGlad to see one more article on fakes > mocks. Here's mine that is not quite as good and focuses on Go from four or so years ago: https://sendgrid.com/blog/when-writing-unit-tests-dont-use-mocks/ https://sendgrid.com/blog/when-writing-unit-tests-dont-use-m...
- williamcotton 4y agoOne approach I’ve found useful is publishing a test suite as a module for use in these kind of “fakes”. For example, a file system blob store that passes the same test suite as an S3 blob store can be used as a reliable fake. https://github.com/maxogden/abstract-blob-store https://github.com/maxogden/abstract-blob-store
- leidenfrost 4y agoInteresting. However, I find mocks useful when I try to replicate error handling in case of unexpected exceptions. How do I replicate that with a Fake? A flag for error triggering?
- sethammons 4y agoIn Go, I create a struct with two properties: err and value. When err is set, it returns that, else returns the value. In the test, I create the fake and set its err to whatever and then verify handling. As needed, you can add properties and add a delay to the response if set.
- SixDouble5321 4y agoAh yes, the test code that you have to write test code for. That's just what I wanted. I imagine there are some situations where the maintenance cost is better after the upfront investment. Seems like betting against one's self to me.