Behavior-Driven Development for Rails: A Preview of RSpec

Ok lately I’ve been busy working on a content management system that works for edge rails or Rails version 2.3. My purpose is to create something simple and something that would work for at least one or two projects. It should not be something I will struggle with or I will grow to hate because of its inflexibility. The beautiful thing about rails is that it’s self-contained but flexible and allows developers to be as flexible and productive as possible.

Much of the functionality needed are done but stopping there would feel like I’m doing a very low level of development. Previously I have been writing unit tests the old way. I was already doing what we call a “Test-Driven Development” because I wrote unit tests while adding code to my models. The process is:

  1. Write the test first.

  2. Run rake test and you will get an error (quite naturally).

        ruby unit/page_test.rb -n test_should_not_save_page_without_title
    
  3. Add your validations/associations or code for your model and run the rake test command again and you should get a better message.

         validates_presence_of :title 
    

This seemed sensible but I’m now using and trying to explore RSpec</a>, a Behaviour-Driven Development framework for Ruby. The RSpec goal is to “write specifications and not just tests.”

An example of a Github contribution that uses RSpec is the Restful Authentication plugin and I’ve learned a lot from the code there.Currently, there is a version working for Rails 2.3. Instead of using the original version by Rick Olson, you can try this.

You can still use Rick Olson’s version and simply remove formatted routes on /spec/controllers/sessions_controller.rb . Take a look at this post. There are no more formatted routes for Rails 2.3.

If you use restful authentication for your app and plan to use Rails 2.3, you’d naturally get a lot of red-colored messages when running rake or rake spec. Most would be related to routes.

Anyway the thing about routing is knowing where to position your routes.

</embed>


You may download a short screencast in mov format showing how to do just that and this also shows you a bit of how RSpec works. Alternatively, you can download the AVI version of the screencast. This is done using gtk-recordMyDesktop. I am too lazy to recompile ffmpeg to make it work so I used mencoder to convert ogv output to avi. I also apologize for the silence.

Interesting Read:

A New Look At Test-Driven Development