In August of 2008, I attended the Agile 2008 conference in my hometown (well almost hometown... go Mississauga) of Toronto. I attended many great talks, met lots of interesting people, spent $150 on books and sat in on 4 great sessions. One of the best sessions I attended was Aslak Hellesoy's talk about Behavior Driven Development (BDD). I was actually hoping for a tutorial style seminar on "This is how you use RSpec...", but what I got was far better than that. We were lucky to see the grand debut of Cucumber. If you haven't heard about Cucumber, then check it out! It's a story testing framework built using Treetop and allows for more concise and expressive user story testing. In fact, it has now replaced the Stories Specs in RSpec.
(Useless Fact of the day:: Aslak named the framework after a sandwich his girlfriend was eating while he was writing the framework).
That night, I went home and tried playing with the framework. Since he had given us essentially an alpha build that day, I ran into a few gem dependency problems that stopped me from moving on that night, but I vowed to get more information the next day.
I must have been wanting to learn this very much since the next morning, I without realizing sat close to Aslak in a fantastic session about Agile lessons and decided to introduce myself after the talk. Aslak, like most in the ruby community, was very friendly and humble. We spoke for a few minutes about the conference and Rails in general and then I brought up my questions about Cucumber. Being a great author and maintainer, Aslak asked if I would pair with him to go over my concerns. I though this was great so we grabbed a coffee and he started showing me the ins and outs of Cucumber and the overall BDD philosophy. We wrote a few features together and he introduced me to the power of Webrat for testing views and controllers. I was really starting to get the idea and when we were done I felt confident that I could apply BDD to my code in several projects.
However, I fell into a common testing Anti-Pattern... I didn't do it! I told myself I would do it on the next project... and then the next etc... This continued until well, yesterday! I have a great idea for a new crowd sourcing application and before I could even name it I was starting to write code... untested code! Then I suddenly stopped in my tracks and had a Eureka moment! The reason I hadn't used Cucumber or RSpec that much in projects was because I was scared! I was actually scared of how much work it would take to learn and master it. When I realized this I realized how wrong that belief was. There had been lots of times when I had started learning something new and with some persistence and hard work I got it! Since I am all about intentions, I decided to set an intention for this project. I intend to not to write a single line of production code until it is backed up by Features and Specs. I will work from the outside in and apply the principals I have learned so much about
So... where do I begin?? The documentation of course! I breezed through the Cucumber Wiki at Github and started writing my first feature. I got that feeling of excitement that I had that night when I first tried it. Each letter I typed was reinforcing the fact that my production code was going to be better and better! I loved the feeling. However, I noticed my myself thinking and feeling "Am I doing this right? Is this how I should write it? What If they use this feature as an anti-pattern" Let's face it! Testing has been said to be hard, so each time you try and do it you are bringing those beliefs and baggage with you... or at least I was. So what to do... First of all, I pushed those out of my mind. Remembered that I am a smart, resourceful person and kept hacking.
I know that Aslak hangs out in the #cucumber channel on irc.freenode.net from time to time, so I decided to go to the source! Upon entering the room I was a little disappointed that Aslak wasn't there, but recognized David Chelimsky so figured I would ask a question. David is also an author of RSpec and an authoritative voice in the testing community, so who better to ask! David was so helpful and spent over 30 minutes with me helping me understand the connection between Cucumber and RSpec. I also learned that he, Aslak, Dave Astels and the RSpec team are writing a Pragmatic Programmer book on BDD, RSpec and Cucumber. While this is not the first Pragmatic Programmer book on unit testing, it is the first Pragmatic Programmer book to focus on Ruby on Rails testing. I can't wait for it to go Beta! It's amazing how nice and open people are if you are willing to listen and learn from them. So if you are really stuck, use IRC and ask insightful questions. Don't go there to post anything, read the forums, read the docs, try it yourself and if you get stuck... head over to IRC to learn from the source.
Anyway long story short... for those with the same testing woes that I had here is my advice. JUST DO IT! Drop out of your comfort zone and start writing Features and Specs TODAY! Not tomorrow, TODAY. Try it on a smaller project where you have full control so that when you have to use it at work or for something bigger, you know what you are doing and feel more confident selling it to peers or to yourself again.
And remember the BDD Pipeline as passed on by David Chelimsky to me yesterday afternoon...
Features > Steps > Specs > Implementation > Specs Pass > Steps Pass > Features Pass > Reliable, Maintainable, More Thought Out, More Beautiful Code.
Start testing your code today! Start using BDD or Shoulda do something to make your code better each day! Make it an intention. Write it down! Set your mind to it. The only thing you have to loose is brittle code.
Kent
P.S. - Great intro talk about BDD and RSpec at InfoQ.
P.P.S. - Also check out Obie Fernandez's The Rails Way with a great set of chapters looking at testing and BDD.