Topic: test(100%) == overkill?

Okay, it's confession time:

I recently finished a small project and there is absolutely no test coverage on it. I can already hear some of you fainting out there, and the ones left standing are freaking out..

"GASP!!! OMG!! But your application is not SAFE! Your code is UNPROTECTED! Only losers dont test first!!!" etc etc

I know ... I know.. I felt really guilty and promised myself that future projects would have test coverage. And for the last 2 weeks, I have made good on that promise.

My first impressions are that Unit Tests are indispensable. My model behavior, once specified is not likely to change that much and tests provide a safety net that allows me to refactor my code much quicker than before.

Where I start to get impatient is with functional tests. When you start working things like redirects into the equation, you start to violate DRY and lose some agility. I don't like the idea that I have to update my code in two different places if the client wants me to make a change.

Another gripe I have is that a lot of Test enthusiasts out there really go over the top. I mean is it really necessary to test things like ActiveRecord validations? Aren't they already well tested? I can understand trying to spec out something like user permissions, but I fail to see the point of saying

specify "should be invalid without username"

When we already know that validates_presence_of :username ALWAYS works.

Are people just obsessive compulsive about testing? Or am I failing to see the value of repeating yourself in a framework that preaches DRYness.

Please help me see the light!

Re: test(100%) == overkill?

We always assume rightly or wrongly that the underlying software, i.e. Ruby or Rails, has been verified by its developers.  So we only test what we've developed.

Re: test(100%) == overkill?

pimpmaster wrote:

Where I start to get impatient is with functional tests. When you start working things like redirects into the equation, you start to violate DRY and lose some agility.

Tests aren't necessarily DRY and that's the point. You need to add some duplication between the test and the real code to do the checking.

pimpmaster wrote:

I don't like the idea that I have to update my code in two different places if the client wants me to make a change.

This is why I like to keep my functional tests pretty slim. A perfect test is one that will only fail if you broke the application. You don't want to go overboard in testing the view for this reason - many times you can break the test without actually breaking the app, you're just changing the layout for example.

However, I often test redirect paths because I consider this part of the behavior of the application. There's often some logic behind why a controller redirects where and it's important that this behaves properly. But, if you're finding this is too limiting, you don't have to test it.

pimpmaster wrote:

Another gripe I have is that a lot of Test enthusiasts out there really go over the top. I mean is it really necessary to test things like ActiveRecord validations? Aren't they already well tested?

I agree with willirl here. No need to test that the Rails framework is working properly, this has already been tested thoroughly by the Rails developers.

pimpmaster wrote:

I can understand trying to spec out something like user permissions, but I fail to see the point of saying

specify "should be invalid without username"

When we already know that validates_presence_of :username ALWAYS works.

This isn't about testing that validates_presence_of works, this is about testing to make sure the user has that defined. So in this case it's not testing Rails framework code, it's testing that the model has the proper validation settings. I usually don't go to this extent but some like to and that's understandable.

What you're seeing here is BDD (behavior driven development). That is, if the application doesn't behave the way you want to (no username is valid) then you should add a spec to specify how you want it to behave. It's not about testing something the framework provides, it's about confirming your application behaves properly.

Railscasts - Free Ruby on Rails Screencasts

Re: test(100%) == overkill?

Thanks for the clarification guys.

I am very intrigued by BDD, especially after hearing about test/spec

The idea of getting functional code without even firing up Mongrel is quite appealing. One day I will be savvy enough to test first.