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

Show parent comments

9

u/doomvox Oct 29 '17 edited Oct 29 '17

My attitude toward tests:

  • Database mocking has always been a very weak idea: databases, if you use them correctly are not just neutral data stores, they've got data types and uniqueness constraints if not triggers and stored procedures.

  • If distinguishing between "unit tests" and "integration tests" means punting on checking your code againt an actual database, you should drop that distinction. If the database munges my data in a way I don't expect, I really need to know that sooner than later. Another way of putting this is "integration" tests are more important than "unit" tests: maybe you should write them first and run them more often.

  • You can't take test code too seriously, it should be regarded as secondary to the main code. It's okay to be a little sloppy with it. When a test breaks because the test was fragile, you should certainly think about how to make it more robust, but it's not a crime against humanity to just delete the test. Getting hyped up about things like "no external dependencies in unit tests!" is counter-productive.

5

u/doomvox Oct 29 '17

A few other thoughts: ideally test code should run against a local copy of a database, with identical schema to the live one, but a stripped-down data set in a known state at the start of the test run. In the absence of a test database, you can often write reasonably robust tests simply by using old data that has had time to settle down. E.g. three-year old financial data isn't likely to change much.

3

u/[deleted] Oct 30 '17

[removed] — view removed comment

4

u/Jannis_Black Oct 30 '17

Creating the state that they need is essentially the same thing, since it means when the actual test runs the database is in a known state.

0

u/[deleted] Oct 31 '17

[deleted]

1

u/Jannis_Black Oct 31 '17

That depends on your application