Getting started with Rails and FitNesse

This is intended to be a tutorial to get you started using FitNesse with ruby on Rails. There is some explanation needed about FitNesse, Fit, and Rails. Smart people have written about such things, I will leave you to Google them as interested. Two good references are listed at the bottom of this tutorial.

So lets get started. Our customer has just written a test that looks like this:

1|vending machine|
4|press|add change|
6|enter|selection|juicy fruit|

This would be a common first fixture for a product, as it is limited in scope, it is a very specific example from which to build a conversation about requirements about. Anyway, the first thing to making this pass is getting some of the FitNesse infrastructure in place around your rails app.

First, lets create a rails app for our vending machine:

1$rails vending_machine

We see all the normal rails stuff, all the creates. Lets download FitNesse and place it in our vending_machine root directory. Then start the FitNesse server.

So, now we go to the FitNesse main page and create the page for our acceptance test. Then put the test from the customer into the page and try to run it. Failures? We need to set up the environment to run with Ruby fit.

Follow the instructions to set up the right test runner for ruby. Lets write our first fixture in a fixtures directory we create in the root folder.

Our new test with the path and test runner set up looks like this:

 1!define COMMAND_PATTERN {ruby -I%p ruby/bin/FitServer.rb}
 4!3 Make one transaction with a vending machine
 9|press|add change|
11|enter|selection|juicy fruit|

There are a few things to notice about the changes:

  • The path is the rails root dir.

  • Fixtures.VendingMachine is mapping to the folder name/module name of where the fixtures are located.

  • If you run the test, you will get all sorts of exceptions, because the fixture code is not written yet.

Now it is time to write a fixture to make this test fail without any exceptions. In the fixtures directory, create a file called vending_machine.rb.

The naming of your fixtures maps with the name of your file, which it will require. Lets make a stub file to make the test fail without exceptions.

 1require 'ruby/lib/fit_helper'
 2module Fixtures
 3  class VendingMachineFixture < Fit::ActionFixture
 4    attr_accessor :currency, :selection
 6    def initialize
 7      super
 8      @@actor = self
 9    end
11    def add_change
12    end
14    def vend
15    end
17  end

Now we should have failures with the amount not changing. Lets create our controller for the vending machine and start to implement the code. I am going to leave out the specs I use to write the controller, just show the code.

Here is the first version of the controller. Note it has the same stubbed methods the fixture has:

1class VendingMachineController < ApplicationController
2  def add_change
3  end
4  def vend
5  end

Lets hook up the fixture to the imaginary controller. There is a fair amount of set up which is rails related. First, we need to load up the environment and the controllers. Add these lines to the beginning of your fixture:

1require File.expand_path(File.dirname(__FILE__) + "/config/environment")
2require File.expand_path(File.dirname(__FILE__) + '/app/controllers/application')

Which should allow us to change the initialize method to:

1def initialize
2      super
3      @@actor = self
4      @controller =
5      @controller.params = {}
6    end

Which creates the controller, and initializes its params to an empty dictionary. There are other ways to do this, like creating test params, but for simplicity, we are going to create our own for now.

Next, we set up the values in the params and call the controller methods:

1def add_change
2    @controller.params[:amount] = @currency
3    @controller.add_change
6def vend
7    @controller.params[:selection] = @selection
8    @controller.vend

Now, we make the test pass by implementing the controller:

 1class VendingMachineController < ApplicationController
 3  def initialize
 4    @items = {"juicy fruit" => 35 , "doublemint" => 45 }
 5    @session = {} if not @session
 6    @session[:amount_entered] = 0.0;
 7  end
 9  def add_change
10    @session[:amount_entered] += params[:amount].to_f
11  end
13  def vend
14    @items.each_pair do |key, value|
15      if(params[:selection] == key)
16        @session[:amount_entered] = 0.0
17      end
18    end
19  end

Missing Requires

There is some weirdness around the way RubyFit collects exceptions after failing to find the file to require. If this occurs, dig into ruby fit’s FixtureLoader, specifically the find_class method. Usually you can get some information from the exceptions being silently caught.


Paul Pagel, Chief Executive Officer

Paul Pagel co-founded 8th Light in 2006, and has been a driving force in the software community ever since.

Interested in 8th Light's services? Let's talk.

Learn more about our Ruby services

Contact Us