Graphic Guide to Rails on NetBeans: Rspec and Autotest from scratch

November 9, 2007

After years of using Eclipse as my Java IDE of choice, it was easy for me to switch over to RadRails/Aptana when I started working in Ruby on Rails. Despite these inclinations, when I heard that NetBeans had Rspec support and a few other niceties, I couldn’t resist checking it out.

Although all of the necessary pieces for getting started with Rails in NetBeans and Rspec/Autotest support are have already been documented in bits and pieces, I thought it’d be useful to lay out a guide for setting this up from scratch. Since IDEs tend to have graphic interfaces, we’ll rely heavily on screenshots.

Prerequisites

Before starting, I’ll assume you have the following installed and working:

Download and install NetBeans 6.0 Beta 2 or newer

At the time of writing, the latest beta release of NetBeans 6.0 can be obtained here. The installation should be straightforward, assuming you have a recent JDK installed.

NetBeans welcome screen

With installation complete, launch NetBeans. Here’s the default interface layout. Nothing to groundbreaking, with the editing pane surrounded by navigation on the left, and output on the bottom.

NetBeans Ruby Gems menu

Now we’ll need some gems, which we may as well install through NetBeans. Go to the Tools -> Ruby Gems menu option.

NetBeans gems install

Go to the New Gem tab. We’ll need rails, ZenTest, ruby-debug-ide and sqlite3-ruby (plus all their dependencies). Select these and Install.

NetBeans gem installation settings

Be sure to include dependencies, and in this case we can just use that latest rails (1.2.5 at the moment).

NetBeans gem successful install

If all goes well, you should see something like this. Repeat the process for the remaining gems you’ll need (ZenTest, ruby-debug-ide, sqlite3-ruby)

NetBeans new project menu

Now to actually create a project. Go to the File -> New Project… menu option.

NetBeans new project screen 1

Choose Ruby on Rails Application, and hit Next

NetBeans new project screen 2

We’ll draw on the original Tutorial in Ruby on Rails for inspiration, hence ‘MyProject’ as a title, but name the project whatever you want. Use sqlite3 as the database unless you have another preference. Make sure the ruby interpreter you want to use is selected. Next.

NetBeans new project screen 3

The last screen in the new project wizard should show that Rails is already installed, if the Ruby Gems steps above worked correctly. Finish.

NetBeans my project

NetBeans has created the skeleton of our Rails app, which can be explored through the navigation pane on the left. It pops open the database.yml so that your database configuration can be defined. No changes are needed if you’re using sqlite2 or sqlite3.

NetBean plugins menu

Now we need to install the necessary plugins so that we can use Rspec. In the navigation pane on the left, right-click on the project name and choose Rails Plugins…

NetBeans plugins repositories
We need to add the repository for Rspec. Go to the Repositories tab, and choose to Add URL…

NetBeans plugins add url
Enter the URL for the Rspec Subversion repository:svn://rubyforge.org/var/svn/rspec/tags/CURRENT. Hit OK.

NetBeans plugins rspec
Now go to the New Plugins tab, and select both rspec and rspec_on_rails. Choose Install.

NetBeans plugin install settings

You have the option of installing the plugins as svn:externals or as an svn checkout. I tend to skip the elegant svn options so that I have the files in my own subversion repository, rather than relying on someone else’s indefinitely being around. Hit OK.

NetBeans plugins installed

Now the plugins have been installed, so we can make use of Rspec in our project. Close.

NetBeans generator

Next, we have to generate some files in our project for Rspec. Right click on the project name in the left-hand navigation pane, and click Generate…

NetBeans bootstrap Rspec

Choose rspec and hit OK. Now we’re all set to use Rspec.

NetBeans generator menu

Now to generate a controller and it’s spec. Right-click on the project name, and pick Generate…

NetBeans rspec_controller generation

Using the rspec_controller will provide us with a controller as well as the necessary spec files. By entering ‘hello index’, we’ll produce a controller named hello with one method called index. Hit OK.

NetBeans rspec files

In the navigation pane on the left, there will now be some files under Rspec.

NetBeans run spec

Open up hello_controller_spec.r. To run the spec, either choose Run->Run File->Run “hello_controller_spec.rb”, or use the keyboard shortcut Shift-F6.

NetBeans spec run results

The output window at the bottom will display the output of running the spec, plus in the editor pane you’ll get a summary that’s either colored green if your spec had no failures, or red if there were failures.

NetBeans autotest started

We can take Rspec a step further if we like, and get Autotest running (assuming you installed the ZenTest gem). Just right-click on the project name in the left-hand navigation pane, and choose Autotest. This will start an initial run through all your specs, and then re-run every time a file is saved.

NetBeans autotest failure

Now we’ll add a trivial spec for our ‘hello’ controller, saying that there should be a ‘message’ assign that says “Hello World”. If we then save our spec, autotest kicks in and says we now have a failure.

NetBeans autotest success

Another nice feature of NetBeans is that you can quickly switch between a spec and the controller/model/view that’s being spec’d, using the keyboard shortcut Control-shift-T. Do this to switch to the ‘hello’ controller itself. Add a line so that @message is set to “Hello World”. Save the controller file, and autotest kicks in again, saying that our tests pass!

So that’s all there is to getting going with Ruby on Rails in NetBeans, with added bonuses of Rspec and Autotest. Enjoy!

Tags:

4 Responses to “Graphic Guide to Rails on NetBeans: Rspec and Autotest from scratch”

  1. Ben Says:

    This is a great walkthrough!

    I’m not seeing any output when running an rspec file. It opens the output window for the test file, but nothing ever appears, nor does the status bar display test results. Any ideas? Rails 2.0.2 Windows XP and netbeans 6.0

  2. Nathan Says:

    You are my HERO!!! I’ve been looking for a MEANINGFUL walk-through like this for several hours now. Kudos!! Up and running 100%!

  3. sal Says:

    I know this posting is kinda old, but when I add the repository and reload the plugins I do not see the rspec nor rspec_on_rails in the list…

    running NB IDE 6.1 on WinXP

    I know rpsec moved to git rather than svn, but is there any way I can do this on XP…?

    any thoughts?

  4. sal Says:

    nevermind…

    changed svn to http and everything worked out great… must be a FW issue on my part…

    thanks much


Leave a Reply