The RSpec Book
A while back… back in November I wrote a post about using Cucumber, RSpec and Behavior Driven Development (BDD) in a Rails project. To sum it up, I was one of the fortunate people who got to see Aslak unveil cucumber for the first time at Agile 2008 in Toronto. It was very well received but I don’t think people really got it. I sure didn’t. I met up with him the following day to go into it in a little more depth. He was nice enough to show me the more Rails centric stuff, but there were some broken features etc, since he had literally just finished writing it. Anyway, I came home feeling pumped from that conference. I felt special since I had been privy to some information that not many people knew about and I was eager to master it. This is not what happened.
In November, I went to Denver for the Pragmatic Studio’s Advanced Rails Studio and learned a lot. I learned a lot more about testing and I also learned about Shoulda. I got the impression from the Pragmatic guys that they weren’t using RSpec and Cucumber so the sheep in me felt like maybe I shouldn’t either. I started playing around with Shoulda and adopted it for my new projects. I loved it. I forgot about Cucumber for a while, stopping every once and I while to watch a screencast or a presentation, but for the most part I stopped using it.
That was until last week.
I am currently re-writing Prospectlinker and have decided that no matter how tempting it is to deploy without some tests, it’s not what I am going to do. I have been bitten too many times passing off tests that don’t seem to matter until you do something like update Rails and spend 2 days finding all of the broken links… anyway, I don’t think I need to convince anyone that testing is good. Joel Spolsky and Jeff Atwood might be the only two developers I have heard talk about why not to test, but they are very smart guys so I am sure they have their reasons. Regardless, I resonate with testing.
I test therefore I code.
Or is it, “I code therefore I test”… anyway, the point of this post is this.: Go back and look at Cucumber and RSpec right now! Seriously, you will be blown away by how much has changed and how much hasn’t, both of which a sign of good software. Also, get your team involved. I recently scared my team to death with a HUGE email explaining Cucumber and how I want them to help me make the code better by writing scenarios and features. I know that they might not get into it right away, but slowly and surely as they get more comfortable with it, they will be writing the features for me and I will be making sure they all pass.
However, I know that it can be daunting. Testing, especially in Rails is like a cult. It can feel like if you didn’t start a while ago you have missed the boat. This is not the case, in fact, there is a boat waiting just for you, and the journey it will take you on is both fun and rewarding, here are some pointers for navigating the course.
- Start small
Testing is a whole side of programming that some never look at. Many talk about it, but very few actually do it and even fewer do it well and regularly. Don’t go and re-test your entire application, start small. Start with a few methods. Using TDD / BDD is a great development tool but you don’t have to do it at the beginning if it seems to intimidating. Do the opposite. Write a method, then write a test to make sure it does what you want it to. Do this for a while and you will see why Kent Beck invented TDD, it’s how you naturally want to write code as you get better and more comfortable with testing. TDD becomes very natural with time and patience.
- Make it your discipline
The word discipline seems to have different meanings depending on who you talk to but for me, it’s all about doing something even if you are less than thrilled about doing it. For example, I meditate twice a day for 20 minutes. At first, this was hard since sometimes I was tired and didn’t always have the energy to do it. However, by applying a simple discipline, it became natural and now flows as part of my day. You can do the same thing for testing. In fact this is exactly what I did. I made a point to start testing each new application or new line of code I wrote. At the beginning it was hard since the payoff wasn’t always visible, however, the payoff is great so stick with it.
- Testing is a journey, not a destination
Testing is not a drop in solution. I have talked to some people who claim that they learned how to test in a weekend, I don’t really believe them when they tell me this. That’s like saying that you read Agile Development with Rails and built the depot application so now you know Rails. You might know about Rails, but you don’t know Rails yet. Software like testing is about the experience of doing rather than of watching or copying. Think of testing as a journey not a destination.
Understand that by practicing testing you are making your software a little better. Even if you write the worst tests imaginable, you are still thinking about your code in a different way. This in itself is very profound and will have a trans-formative effect on how you write code and even how you think about writing code in the future. Try to remove your ego and that annoying voice in your head telling you that you should have learned this stuff months ago, that you are a sub prime programmer since you don’t know all there is to know about testing. That voice is just trying to protect you but don’t listen to it. Let it go and embrace the experience of making your code better. When you don’t resist not only will love it more, but you write better code naturally without thinking.4. Have fun with it
Make testing a game. This what I used to do at the beginning when I was first learning how to test. Find a friend who is willing to review your code and give you a grade, or write some code with a test and then write some without a test. Get someone to grade which one is better, or if you can’t find someone, send it to me and I will grade it. Chances are the one that was done with TDD/BDD will be better or cleaner than the latter.
- Ask for help
Go and download your favorite IRC client (looks like Colloquy is winning for the Mac) and connect to irc.freenode.net and join the #cucumber #rails and #rspec rooms. I have learned so much about testing from dchelimsky (David Chelimsky) in those rooms that it makes me smile :) The whole team behind these tools are great people and just like you, are learning all the time.
For those that want to help themselves, you really should buy the RSpec book too (see top image). The Pragmatic Programmers have a way of finding the most amazing talents and produce the highest quality software books. If you are going to buy it, buy directly from them instead of through Amazon, that way they get to keep all of the profits and use them to make more amazing books.
Well, I think that about does it.
I am certainly not the expert on testing, but I have been doing it enough to know my way around. Also, having been recently recharged into Cucumber, I am looking forward to learning more and more until it becomes as natural as writing Rails.
Feel free to ask questions in the comments, or contact me directly if you have any questions, comments or concerns. If I can’t answer it, I am sure that I will find someone who can.
A big thank you to everyone who has worked on these tools and continues to make them better, we are very lucky to have you and your software.