I tweeted this a few minutes ago:
This is in reference to a horrible test that I wrote today. It’s got 2 assertions and more than 20 lines of context to set up the mocks that I needed, to isolate things and prove what I wanted to prove.
As a reference point, here’s the implementation the class under test:
In the same thread of thinking that Jimmy has been posting about recently (which I’m in 100% agreement with), I want to get away from writing ugly tests like this. I’m clearly breaking all the principles that Jimmy is talking about – I’m exposing a lot of detail of the implementation of the class under test, I’m creating a very brittle test that will break with any small change to the class under test, and I’m making it very painful for me to change this part of the system because the tests are getting in my way.
Here’s the problem I have with a lot of the talk concerning bad tests… it’s wonderful talk, but it’s nothing more. I’m reading Jimmy’s posts and I’m thinking that he’s on to something, but I’m always left with a giant question at the end: how do I _not_ do what you’re talking about?
What are the alternatives to this? How can I improve my test, in this scenario? Am I missing something simple like an object mother or a context class? Should I be letting the actual classes run in this case, which would require a fairly significant amount of setup data including a number of .haml template files that need to be read / processed?
I’m looking for real-world examples of how to solve real-world testing problems. It doesn’t really matter what language the tests are written in: Ruby, C#, Java, Python… I’ll even take C++ if your solution is understandable and illustrates the principles of good test design.
Post links to your solutions to this type of problem in the comments.
Post Footer automatically generated by Add Post Footer Plugin for wordpress.