Don't ignore instincts

A few days ago, I wrote this post on testing more. So I started off with some integration testing my http handlers. There was one huge flaw in the way I was testing, it's the eternal elephant in the name of, mocks. Long story short, mocks are garbage. It took less than a week to realize that, in fact it took three days. But today, it was in the pudding. The whole time, I was writing the test, my instincts were like, what is the point of this? But I thought it was just the discomfort of writing tests, itself. Turns out, it wasn't. When I'm actually testing stuff (unit mostly) it does take some thinking, and the end result actually does help. Plus they are not flaky, as long as the method is correct, they continue to work. Small changes to some method name somewhere, does not break the tests. Like they do when you change your method names, but the mocks are not updated yet.

So what really happened? Today, I was pushing stuff to production, and I came across a peculiar bug. Every time I would refresh the page, the session values were empty. At first I thought it was session related bug, cause when I tested the same on localhost it worked fine. Turns out on the bug was introduced recently as I was refactoring the register handler. I was confident in my refactoring as I had "tests" that were passing, so I kept going. But turns out, when a user signed up and before registering their organization, the system was in a weird state and the database would work as expected but the session info would not. Anyway my point is that, in my tests unless I thought of this ahead of time, which is not how bugs work it turns out, the mocks would have caught it. On the other hand, if I had used a test database to run the handlers against, it would have been caught. So in the end all the mocks did was make me use interfaces for every dependency, so I went ahead and confidently deleted all my mocks and removed all the garbage interfaces. Good riddance.

The worst part is, when my lizard senses started tingling, I had the good sense, to verify if mocks are a good idea, most of my trusted sources were like hell no, I guess you really learn some lessons the hard way.

Fuck mocks.