4 ms·
what is the place for mocks?
by convolvatron 4y ago
what is the place for mocks?
- Tainnor 4y agoExternal systems, unexpected error cases (which by nature of being unexpected can't really be triggered in a "natural" way), very generic code (e.g. instrumentation frameworks), major architectural boundaries, ... I agree that most people over-mock, though.
- readthenotes1 4y agoExternal systems dependencies are better handled with stubs and fakes than mocks, in my experience.
- Tainnor 4y agoThat really depends on the case in question and on the particulars of the codebase. I wouldn't recommend mocking http libraries, database calls etc. - that usually leads to madness and impenetrable test setups. I would rather recommend to either use fake implementations (e.g. in-memory DB) or to only integration-test the boundaries of your interaction with the external system (that might or might not be hard to achieve). But I do like the pattern of wrapping calls to external APIs through a layer that I control - so that, for example, I may just write mailer.sendMessage(message) - and then that mailer interface seems like a good candidate for mocking.
- readthenotes1 4y agoIn my experience, they are the test double of last resort. I used them to verify behavior, not outcome (as that's what expectations do), e.g., did I really call this method only once?