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|
2 |check|amount|$0.00|
3 |enter|currency|$.35|
4 |press|add change|
5 |check|amount|$.35|
6 |enter|selection|juicy fruit|
7 |press|vend|
8 |check|amount|$0.00|

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}
 2 !path 
 3 
 4 !3 Make one transaction with a vending machine
 5 
 6 !|fixtures.VendingMachine|
 7 |check|amount|$0.00|
 8 |enter|currency|$.35|
 9 |press|add change|
10 |check|amount|$.35|
11 |enter|selection|juicy fruit|
12 |press|vend|
13 |check|amount|$0.00|

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.

 1 require 'ruby/lib/fit_helper'
 2 module Fixtures
 3   class VendingMachineFixture < Fit::ActionFixture
 4     attr_accessor :currency, :selection
 5     
 6     def initialize
 7       super
 8       @@actor = self
 9     end
10     
11     def add_change
12     end
13     
14     def vend
15     end
16    
17   end
18 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:

1 class VendingMachineController < ApplicationController
2   def add_change
3   end
4   def vend
5   end
6 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:

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

Which should allow us to change the initialize method to:

1 def initialize
2       super
3       @@actor = self
4       @controller = VendingMachineController.new
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:

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

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

 1 class VendingMachineController < ApplicationController
 2   
 3   def initialize
 4     @items = {"juicy fruit" => 35 , "doublemint" => 45 }
 5     @session = {} if not @session
 6     @session[:amount_entered] = 0.0;
 7   end
 8   
 9   def add_change
10     @session[:amount_entered] += params[:amount].to_f
11   end
12   
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
20 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.

Resources

Paul Pagel, Chief Executive Officer

Paul Pagel has been a driving force in the software craftsmanship movement since its inception.