DEV Community

Discussion on: Why Mocks Are Considered Harmful

Collapse
 
jesterxl profile image
Jesse Warden • Edited

2nd post from you in my feed, and I wholeheartedly agree with this one as well. I still think having basic stubs/mocks for unit tests are good if you practice Test Driven Development. The point is for design, not just "does the code work". The key would be just to use dependency injection in OOP or "passing parameters to functions in FP". In your example above, that'd be database_write being a function passed in; stub in unit tests:

def database_write_stub():
  pass
Enter fullscreen mode Exit fullscreen mode

Then use it:

def test_logic():
  assert logic(database_write_stub) == 5
Enter fullscreen mode Exit fullscreen mode

And a real function in integration.

I hope you keep writing articles like these.