← Back to context

Comment by languagehacker

12 days ago

I don't think mocking is an anti-pattern. Using only unit tests and then mocking everything probably is.

Mocks have a perfectly viable place in testing. They help establish boundaries and avoid side effects that are not pertinent to the logic being tested.

I would reference the testing pyramid when thinking about where to be spending time in unit tests vs. integration tests vs. end to end tests. What introduces risk is if we're mocking behaviors that aren't being tested further up the pyramid.

I think the better advice that goes with the spirit of the article is, prioritize integration testing over unit testing if you're constrained on time.

I like the testing pyramid specifically because it captures the tradeoffs between the different kinds of tests. Mocks can come in handy, but like anything else can be abused. We need a "Mock this, not that" kind of guide.