Tuesday, June 2, 2009

RSpec and AutoSpec with JRuby

I had a little bit of a struggle last weekend setting up RSpec with JRuby so I thought I would share the way I set this up. I am not sure why I could not find more information on how to do this so I thought I could help the next guy. Anyways, I am using JRuby 1.3.0RC2 which comes bundled with RSpec 1.2.6 which is great but there were two issues I needed to address before using this in my Rails application.

The first issue was introducing RSpec into my Rails application. It turns out that this is simple. First you need to install the RSpec-Rails gem like this:

jruby -S gem install rpec-rails

Next switch to your Rails project home directory and scaffold out the RSpec components like this:

jruby -S script/generate rspec

This will introduce an rspec folder with the initial scaffolding you will need to write RSpec tests for your Rails application. This only needs to be done once.

The next thing I wanted to do was use AutoTest so that my tests would run in the background while I am coding. This way I can make changes to my code and tests and have the modified tests run on a continuous basis. When we ran the RSpec scaffolding in the Rails application we received an autospec script in the script folder. This is what will allow you to have this perform your local continuous testing. The only other gem you will need is the ZenTest gem which is used for AutoTest and AutoSpec.

jruby -S gem install ZenTest

You are now setup to run all of your Rails RSpec tests manually with Rake from your project home directory:

jruby -S rake spec

Or you can turn on AutoSpec and let this happen in a Terminal like this:

jruby -S script/autospec

I am also using the latest NetBeans IDE (6.7 RC1) for development and find that AutoSpec works great there as well. To do this right click on your project folder and click AutoSpec. This will kick off the script. Here is an example of some of my results:




NetBean also has a nice navigator window for seeing an outline of your specs:


I think that RSpec and AutoSpec provides a very easy and productive way to practice TDD in your Rails applications. Well back to writing more tests...

No comments: