HOWTO: Integrate RSpec on Rails with jEdit

Here’s how to hack together a setup to load an RSpec backtrace into jEdit. In other words, you’ll be able to click the relevant failed behaviour or error to go straight the relevant file and line.

Why jEdit? The pros:

  • Java-based, so you can share the same config across many different platforms
  • wildly extensible and powerful - perhaps even beating Textmate on this front
  • free and open-source
  • fairly large ecosystem of plugins, macros etc.

Essentially, I haven’t found anything I need that the big IDEs or Textmate do, that jEdit can’t do or be made to do: snippets, "IntelliSense", projects, columnar editing and so on. It strikes the right balance between, say, Notepad++ and RadRails.

jEdit’s drawbacks:

  • throws the occasional strange bug, sometimes requiring closing and reopening
  • Java-based, not native to the Windows, so is not as slick as some other editors (no drag & drop for files/folders, shonky Swing UI)

I like e (£15 is a steal) but I don’t really want to run a Cygwin stack. I also was having real trouble making some of the ported bundles (like RSpec) working. But I haven’t given up on it.

Anyhow, if you’re still reading, you’ve probably got jEdit now. Jolly good.

Setup

Install these jEdit plugins

  • Console
  • Error list
  • Project viewer

The easiest way to do this is through jEdit’s plugin manager itself. You may have to close and reopen jEdit for the plugins to appear and start working.

Setup Project Viewer

You need to have your app organised into a project using the Project Viewer plugin. This will only work if it is. There are plenty of other benefits to working like, too, like FastOpen.

Setup Console

The Console plugin includes a little GUI front-end for commands, called Commando. I’ve made one make one for RSpec.

  1. Find out where your jEdit user settings folder is. On Windows, it’s normally %userdir%/Application Data/.jedit. To double-check, open a jEdit console window and enter %echo ${JEDIT_SETTINGS}.
  2. Save this file as ‘rspec.xml’ in YOUR_JEDIT_SETTINGS_PATH_GOES_HERE/console/commando.
  3. Close and reopen jEdit. Select Plugins - Console - Commando. A popup opens and you should be able to see an RSpec entry in the drop-down menu at the top. Check you can.

Create a new Console error scheme

  1. Select Plugins - Plugin options - Console - Error Schemes.
  2. Click the plus button at the bottom to add a new error scheme. Call it something ravishing, like "RSpec".
  3. Enter this information in the relevant fields.
  4. Make sure you hit "test/commit" once it’s entered, otherwise you’ll lose your changes. Duh.

You’ll see that failing behaviours will be treated as errors by the Error Scheme, but Ruby errors will be handled as warnings. Nothing is meant by this, only it’s nice to have them appearing in different colours.

Add custom RSpec formatter

Save this file as custom_rspec_formatter.rb in your Rails lib folder.

Actually, it doesn’t matter what you call it or where you save it. It’s just that, by default, the RSpec commando will require a file called "customer_rspec_formatter" from your lib, so you may as well call it that.

This custom formatter makes the output from RSpec more amenable to Error Scheme. In particular, it makes the regexes much easier to write!

Assign keyboard shortcut (optional)

  1. Select Utilities - Global Options - Shortcuts
  2. Select ‘Commando commands’ from the drop-down
  3. And so on.

Other requirements

Obviously, your RoR project should have the RSpec and Rspec on Rails plugins installed.

I am running:

  • XP SP2
  • Jedit 4.3pre9 using Java 1.6.0_05
  • RSpec 1.1.2

Haven’t tested it on anything else.

Usage

  1. Start spec server in a separate shell: ruby script/spec_server start works for me on Windows
  2. Open your app as a project through Project viewer
  3. Run RSpec commando. You should see some output in a jEdit console window.
  4. Open error list (assuming there are some errors)
  5. Clicky error.
  6. Profit.

Note: it appears, looking at Commando, that running the spec through the spec_server is optional. At the moment, it’s not.

Todo

Yup, it’s all a bit hacky. Here’s what I’d like to do next:

  1. Update the commando definition to allow:
    • Run focused behaviour
    • Run model, controller, or view specs
    • Run app specs
    • Run all specs (including plugins)
  2. Update the commando definition to allow user to chose a custom formatter
  3. Handle pending behaviours as warnings, and colorize Ruby errors/failed behaviours thru RSpec

Hope someone else finds it useful. Post issues below, please.

Say something

Questions and comments are welcome. You can use simple HTML and include links.