I’ve been really putting off using Rspec. Unfortunately ( and like the vast majority or programmers out there) I have not been writing enough tests for my apps. When I did write them, they were using Rail’s built in Test::Unit library, which, many will say… sucks. Plus, honestly, they were half assed.

So, I’ve taken it upon myself to dive into Rspec. Most everyone agrees it is the way to go, and it gets you into BDD, which gives me another bussword I can throw on the resume (/sarcasm).

The part about writing tons and tons of tests to cover all your app that didn’t make sense was that you would end up writing so much more code. In reality, I think it boiled down to being lazy, which all programmers are. Since the Rails community prides itself on keeping things DRY and keeping the amount of code written to a minimum, why the hell would I want to write tons of tests for stuff I don’t think are going to break?

In the long run, for any programmer, it is really not about how much code you actually have to write, its more about how much time you send making the app work. If you’re like me, there have been many nights spent debugging some controller that totally broke and you’re stuck refreshing your browser, creating a user, deleting a user, and checking the log over and over until you solve it. I hate it and have probably wasted days doing this. If you had written tests for your app, starting with that first controller, and continued writing quality tests, used autotest, you would avoided this. You would be confident that you code was rock solid.

What about if you’re absolutely sure you’re code won’t break? Well, it will. Maybe not all of it, but you’re gonna have problems eventually. I don’t think that writing tests for trivia stuff (like validating that validates_presence_of is working) is really worth while, but other stuff is fair game.

The list point I’d like to make is that I think since really writing tests and using Rspec, my thinking has changed. I know think writing code, not just as how do I make this work, but also, how to I ensure this WILL work. You’ll find yourself trying look for ways that your code will break, looking for more flaws, and consequently, leaning from those flaws. I’ve found myself refactoring much more code and that end up making my code more flexible and cleaner.

I don’t want to pretend to be a captain awesome when it comes to writing tests yet, but I really think I’ve been coming along very well and am so glad I’ve been investing so much time in it. Give it a try ya’ll.


Leave a Comment


To insert code, wrap in <pre></pre> tags.



Recommend Me

John Yerhot is Digg proof thanks to caching by WP Super Cache!