Randomness for you since 1976
Testing. For most developers, it lurks like a cancer in the back of our brains. You know you should be writing tests, and you should be writing them now, but… well, let me just get this thing working first. At RailsConf I attended several talks, and a tutorial, on testing. While I didn’t get much in the way of concrete knowledge to get started writing tests, I did drink the testing Kool-Aid and I am on my way to becoming a Test Driven Development/Behavior Driven Development advocate.
All of that said, I don’t have enough experience writing tests to be an expert, but this conversation I had today seemed worthy of sharing. When do you use a mock. I uttered a phrase that received a lot of head nods towards the tail of the conversation:
“Mocks are bug aggregators”
If you decide to use mocks in your testing, you are essentially saying “I have no way of testing this for real” or “This is too hard/time consuming/boring right now”. That isn’t to say that mocks dont have their uses, like mocking data that comes to or from a remote system, but they don’t actually test true functionality. If they did, we wouldn’t call them mocks.
What a mock does is create a choke point for aggregating bugs. If you don’t have time to write tests for part of your system, you can mock it, and then when bugs arise that slip past your tests, you have a good idea where to start looking. Of course, good little programmer girls and boys will write tests instead of mocks, but sometimes reality usurps idealism.
This blog is a dumping grounds for my experiences as a web developer, a parent, an artist, a writer and a human being. Maybe someday there will be something here that is worth your time, some sort of useful information or words that piece your very soul. Probably not though.
Jason E. Rist
June 7th, 2007 at 11:35 am
I’ve encountered this recently at work as well. From my experience Mocks do have their place, but they seem to be in what you mentioned - simulating lookup data from a database. Until we get a chance to rewrite our code to use HSQL and then separate Hibernate from the database, we have to use Mocks for our DAOs. (Side-notes: we’re writing the tests for code that is already in place and haven’t yet gotten to the Mocks for the DAOs. I’m sure it will be painful soon!)
Catherine
June 11th, 2007 at 11:23 pm
hallelujah on separating hibernate from the database
I had worked very happily not knowing this beast. Once I met it, I can’t say we’re friends. But then again, I think people - namely your friendly database architect - should control the schema, not what make hibernate the happiest.
Not that that was Morgan’s point at all, I at least know that much.
Peter Williams
June 15th, 2007 at 11:37 am
Mocking…
Mr Whitney recently posted an article in which he described mock objects as “bug aggregators”.
I once held a similar point of view.
Back then my belief was that test doubles (mock, stub, etc)
should only be used when real
objects would not …