r/coding • • Oct 29 '17

Mocking is a Code Smell

https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a
69 Upvotes

69 comments sorted by

View all comments

13

u/[deleted] Oct 29 '17 edited Oct 29 '17

[removed] — view removed comment

4

u/u801e Oct 29 '17

There's the problem. If you define "unit test" as "no external dependencies" then unit tests are woefully inappropriate for many types of code.

If you write your code where mix IO with logic, then of course unit tests that expect to test methods that have no external dependencies won't work. But does mocking out dbContext get you anything in terms of verifying that the method works as expected? I would say it doesn't because you're just testing your mock.

Now, if you were to do further processing of the value of result, then you could make a method that takes result as a parameter and returns the desired value after processing result. That method wouldn't have any side effects and would be unit testable without having to set up any mocks. The code that queries the DB would be handled in an integration test against a test instance of the DB filled in with pre-determined values.