MVC & BDD or “How I learned to stop worrying and love the test” July 13, 2010 at 5:38 pm

For a long time now, I’ve known I needed to get better at testing. Not smoke testing, but real unit testing, test driven development (TDD), or better yet behavior driven development (BDD).  I’d read papers, API’s and even wrote some after-the-fact unit tests for a CakePHP project.  It always seamed overly laborious and time consuming.

My second-most recent project at work is a fairly complex/robust Ruby on Rails application which consumes and provides a front-end for our REST services.  Under the gun to get features out quickly, we skipped over BDD and went right for a system of “live prototyping”. Devoid of tests, this app quickly became a game of Jenga where fixing one thing jeopardized the functionality of another. Implementing Cucumber tests after the fact stopped some of the bleeding, but I was finally starting to see the light.

Which brings me to my most current project. Also a Ruby on Rails application, I had the chance to do ground-up development so I decided to do it right.  At the suggestion of a co-worker who knows better, I replaced Rails’ default testing framework with RSpec for true BDD. The going has been slow, but here’s what I’ve found:

  1. By starting with behavior I’ve stuck to the “fat model, skinny controller” model where most of my functionality is in the model. It’s easier to test functionality on a model and then string the resulting data together in the controller.
  2. After the initial delays from learning rspec and getting the initial models and model tests written, the views come together really quickly
  3. I often re-factor my methods several times durring my initial writing. Using Autotest not only checks that I update all affected code, but I start anticipating where I need to update much faster than if I were doing it solo.

I hope someone comes across this article and it provides the final push to get on board with BDD. I truly believe that in the last month of development I’ve improved my MVC development skills as much as I have over the past 3 years. It’s provided a lot of “a ha” moments, and I’m writing better code.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • StumbleUpon
  • TwitThis

Comments are closed.