4

I read this answer which tells Mockery is an anti-pattern of TDD. But wait! We have to mock/stub all dependencies to write a unit-test. In other case it would be an integration test.

Community
  • 1
  • 1
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • 2
    That's not what that answer says at all. It just says it's a tool to be used with care. That doesn't make it an anti-pattern. – skaffman Aug 16 '11 at 08:24
  • In my experience, this is a bit more of a problem with mocks than with stubs. You have to satisfy your dependencies, but most dependencies should not do anything, especially not test conditions with are irrelevant for the test case at hand. I'd say this is the most dangerous aspect of the Mockery, because it changes the way you think about the tested objects. The fragile web of dependencies itself can be resolved by refactoring most of the time. See Martin Fowler's article http://martinfowler.com/articles/mocksArentStubs.html for more details about mocks and stubs. – Malte Clasen Aug 16 '11 at 08:56

1 Answers1

6

Overusing mocks can be an antipattern, not mocking itself. It's true that you need to mock/stub your dependencies, but when your class has too many dependencies, it can take more time and effort to mock and wire up everything correctly than to write the class itself. And the mocking definitions are very fragile and easy to break, what is somehow against TDD rules.

Too many dependencies means either that your class is some kind of god object that needs to be refactored or is some kind of top-level coordinator that do not have any special logic on its own (like only sequence of dependency calls), in which case I would say it is not really needed to unit test it, when all its components are unit tested well (integration tests can cover this nicely, unit test will be mocking hell without any real value).

NOtherDev
  • 9,542
  • 2
  • 36
  • 47